Created
April 29, 2012 09:42
-
-
Save klovadis/2549052 to your computer and use it in GitHub Desktop.
How to correctly raise errors in node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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