Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to correctly raise errors in node.js
// example function that raises an error
function example (callback) {
// correct approach
return callback ( new Error('an error occurred') );
// NO! BAD KITTY!
return callback ('an error occurred');
} // some_function()
// call example function
example( function (err, result) {
// if you passed an error object, this will give
// you additional information about the error, i.e.
// the stacktrace. If you only passed a string,
// you would now only see that string.
if (err) console.log(err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.