pass-error

Continuation based error passing

npm install pass-error

Pass Error

Build Status

This module implements the pass pattern

You can use it to turn all youre node.js code that looks like this

pass  = require("pass-error");
redis = require("redis").createClient();
function getLatestNews(next) {
    redis.lrange("news", 0, 25, function(error, value) {
        if (error) {
            next("failed fetching news");
        } else {
            next(false, value);
        }
    });
}

Into this

pass  = require("pass-error");
redis = require("redis").createClient();
function getLatestNews(next) {
    redis.lrange("news", 0, 25, pass(function(value) {
        next(false, value);
    }, next));
}

pass(pass, fail, customError = null, context = undefined)

  • pass
    • This is the callback that will be invoked when no error is present. The error argument will be dropped from the arguments it receives as we've already verified that it is not an error
  • fail
    • This is the callback that will be invoked when an error is present. The arguments will be untouched and will be the typical (error, values...) convention
  • customError
    • If this value is non null then it's value will be used to replace any error values that would be passed in otherwise
  • context
    • This is an optional object context to bind to
npm loves you