errup

0.2.3 • Public • Published

errup - magic error handling for node.js

All hates "callback hell" in node.js. With this module you doesn't need check 'err' every callback. Callback error will be checked automaticly and will be rised if it needed.

Old style(without errup)

function readDirectoryAsync (path, callback) {
    fs.readdir(path, function(err, filenames){
        if (err) {
            callback(err);
            return;
        }
        // do something there
        console.log('Success');
        callback(null, filenames);
    });
}

New style - with errup (JavaScript)

errUp = require('errup');
function readDirectoryAsync (path, callback) {
    fs.readdir(path, errUp(function(filenames){
        // Error check and rise to callback automaticly in errUp on error.
        // This code is called only when no error given.
        // do something there
        console.log('Success');
        callback(null, filenames);
    }));
}
 
// express sample
app.get("/", function(req, res, next){
    readDirectoryAsync(__dirname, errUp(function(filenames){ // error will be raised and called next(err) on error
        res.send(filenames);
    }));
});

With CoffeeScript

errUp = require 'errup'
app.get "/"(req, res, next) ->
    readDirectoryAsync __dirnameerrUp (filenames) ->
        res.send filenames

Can also set error handler callback

May be need if last function in stack is not errUp(eg closure, or other)

app.get "/"(req, res, next) ->
    do () ->
        anyAsyncFunc errUp next(ret) ->
            res.send ret
 
anyAsyncFunc = (callback) ->
    fs.readdir __dirname(err, dir) ->
        if err? return callback(null[]) # skip error 
        async.map direrrUp callback(stat) ->
            return next nullstat
        errUp callback(statResult) ->
            // .. do something with statResult
            callback nullstatResult

Can also catch some errors

LockError = (@message) ->
    @name = "LockError"
LockError:: = Error::
 
readDirAsync = (callback) ->
    fs.readdir __dirnameerrUp (dir) ->
        for path in dir
            if path is "lock"
                return callnack new LockError("lock was found")
        callback nulldir
 
readStatus = (callback) ->
    fs.readFile "status"errUp (data) ->
        if data.langth > 1000000
            return callback "many"
        return callback nulldata
 
 
readDirAsync errUp (dir) ->
    # error "many" will raised, and can be catched 
    readStatus errUp (status) ->
        console.log(dirstatus)
[LockError'many'](err) ->
    if err is 'many'
        console.warn "Panic! Get many."
    else
        console.warn "Lock found.. Delete it."
        fs.unlink "lock"
 
# catch all errors 
readDirAsync errUp (dir) ->
    # error "many" will raised, and can be catched 
    readStatus errUp (status) ->
        console.log(dirstatus)
null(err) ->
    console.error "Error catched"

Readme

Keywords

none

Package Sidebar

Install

npm i errup

Weekly Downloads

3

Version

0.2.3

License

MIT

Last publish

Collaborators

  • mikxail