generic Error handler MEANJS, mongoose, winston generic Error handler MEANJS, mongoose, winston mongoose mongoose

generic Error handler MEANJS, mongoose, winston


I can help you with my setup that I'm using after I did some investigation by inspecting the code of Ghost and Mongoose itself.

My workflow is making custom error class that look something like this ( actually I took the idea from mongoose custom errors .

var AppError = function (err) {    var name = "Application Error", msg;    if ( err instanceof Error ) {        msg = err.message;       name = "Application Error [" + err.name + "]";    } else {       msg = err;    }    Error.call(this);    Error.captureStackTrace(this, arguments.callee);    this.message = msg;    this.name = name;    // Here is the interesting part    console.log('An error occured', this);    // Or even let's pretend smtp is our mail module    smpt.send('admin@site.com', err);}

By using this I always do something like :

dbUser.find(query, function(err, data) {    if ( err ) return new AppError(err);    // Do other stuff.});

I improved more this code by using Promises. ( Actually I took this from source code of Ghost ).

Mongoose by default also support promises. So simply you do something like this :

var wrapError = function(res) {    return function(err) {       new AppError(err);       res.send(500, "Internal server error");   };}

And later in your query code

dbUser.find(query).exec().then( function(data) { console.log(data) }, wrapError(res) );

I hope this can help you figure it out further.