Skip to content

Instantly share code, notes, and snippets.

@klovadis
Created April 29, 2012 09:42
Show Gist options
  • Save klovadis/2549052 to your computer and use it in GitHub Desktop.
Save klovadis/2549052 to your computer and use it in GitHub Desktop.
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