1

I'm following the null first error handling convention for Node JS. Callbacks are done as callback(null, response). After receiving the callback response, do I have to check if error is null every single time?

Example:

methodCB(function (error, response) {
    if (error) {
        //handle error
        //Do i have to check this every single time for a callback?
        //Are there design alternatives for this or is this just a node thing?
    } else {
        //process response
    }
});
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
user3808357
  • 241
  • 1
  • 4
  • 13
  • 1
    Are you somehow used to programming that doesn't handle errors? Yes, all robust code pays attention to any possible error return - every single time! There are different strategies and tools for error handling (like promises for example), but any good strategy has code to deal with all situations that might produce an error. – jfriend00 Jul 12 '15 at 08:03
  • @jfriend00 I was unsure if my approach was a correct one as i thought i was writing repetitive code. Thanks for the info – user3808357 Jul 12 '15 at 20:36

1 Answers1

4

As usual, errors can be either operational errors or programmer errors.

The choice between handling and throwing of these errors is a a fairly in depth topic. The canonical guide on error handling in node.js is Node.js Best Practice Exception Handling which I strongly suggest you read. The article highlights situations where you should specifically handle errors, and others where you should not (instead, you ensure that your app fails gracefully).

Regardless of the origin of the error and the choice of handling/throwing, as jfriend00 suggested, all robust code must pay attention to any possible error return.

Community
  • 1
  • 1
snozza
  • 2,123
  • 14
  • 17