I'm coming from a python background. In python, it's common to throw an exception when something goes wrong. If you know how to handle the problem, you can recover from it. If not, the execution is aborted.
In web, this means that if the request presents a failure, like a database query gone wrong for instance, the exception will bubble up to some higher up code that may return an error 500 to the users and log the error (in sentry, or directly to email, etc). The bottomline is: (almost) no error gets unreported.
In backend with node, however, I've noticed it's a pattern to pass the error as a callback argument. This effectively makes the programmer responsible to take action every time, which I consider error prone. Just playing with it a little got me frustrated to see messy code running without doing anything, not even reporting an error.
Of course we should handle every error, but we are human beings, right? What if I forget some? How can I make sure that this error I forgot will make it's way into my email box, so I can fix it?
I think that, JavaScript Promises can help you html5rocks.com/en/tutorials/es6/promises gist.github.com/jakearchibald/0e652d95c07442f205ce
I really like error-propagation via Promises. Under the hood, I throw for sync functions and use promises for async functions, but nearly all higher functions use Promises and I really recommend making strictly sure that all errors which might happen are propagated to the highest level possible.
By the way, I do not use wrappers. I just use the functions and methods as documented, but when I call them, I try to bring them into my scheme.
Examples:
// Promises
var defer = require('promise-defer');
/**
* Does something
*
* @return Promise
* int on success
* error on fail
*/
function foo() {
var d = defer();
// Yes, in this case you could just return the result directly, but for clarification I will leave it like this
doAsync().then(d.resolve, d.reject);
return d.promise;
}
// Error Callback
var defer = require('promise-defer');
/**
* Does something
*
* @return Promise
* int on success
* error on fail
*/
function foo() {
var d = defer();
doAsync((err, result) => {
if (err) {
d.reject(new Error(err));
return;
}
d.resolve(result);
});
return d.promise;
}
// Throw
var defer = require('promise-defer');
/**
* Does something sync
*
* @throws string
*/
function doSync() {
throw 'No catnip :(';
}
/**
* Does something
*
* @return Promise
* int on success
* error on fail
*/
function foo() {
var d = defer();
try {
d.resolve(doSync());
}
catch(err) {
// err is already an Error Object
d.reject(err);
}
return d.promise;
}
That's a framework thing. In express and the http-module you handle errors with callbacks (event or direct). But Koa and Spirit are promise based. So you can throw errors.
Volkan Çakıl
Deus ex coder
Three main ways to deliver an error in Node.js
i guess you are looking for something like that:
const EventEmitter = require('events'); class MyEmitter extends EventEmitter {} const myEmitter = new MyEmitter(); process.on('uncaughtException', (err) => { console.log('whoops! there was an error'); }); myEmitter.emit('error', new Error('whoops!')); // Prints: whoops! there was an error