Live data from Hacker News

Node.js error handling

snmaynard.com

21–24 of 24 posts

Re: Node.js error handling

#21

Earlier quoted context omitted.

One of the things I love about node.js is that it forces you to come up with a style and stick with it for things like this. At http://ratchet.io our API servers are written in node.js and we chose the foo(err, callback) method. I personally love this style even though it makes you write more boiler-plate code, it forces you to write exception-safe code from the start. Also, it forces a structure on all of your code…

How do you deal with code where the author forgot to handle all of the failure cases and invoke the callback?

Fix the code.

Re: Node.js error handling

#22
post #12

I actually just wrote a Node + Express + Backbone error handling system today. I've been staying sane through the use of the 'Q' promise package ( https://github.com/kriskowal/q ). For example, in the below code chunk any errors thrown in makeEmailConfirmation or sendEmailConfirmation will land in the .fail() catch block. I believe using a promise system is the only way to avoid callback hell. var deferred = Q.defer(…

You can also use fibers and make the code appear synchronous, which gives you the ability to use exceptions for error handling.

Re: Node.js error handling

#23
Will these examples protect you from thrown exceptions? Null reference exception? Oops, your callback isn't getting called -- and you're probably leaking resources or leaving things in an inconsistent state.

So the recommendation is to restart the process - but let's not forget that hundreds+ of other requests could currently be in the pipeline - are we just going to abort those too?

Ok, we'll clearly the solution is to wrap all our callbacks with try/catch! Shoot me now...

For large, sprawling applications used to support a business, something like https://github.com/scriby/asyncblock can resolve most of the headaches with async code, including error handling. It's worked out well for my team at work, which is building a "very large" node application.

Disclaimer: Asyncblock is my pet project.

Re: Node.js error handling

#24
post #12

I actually just wrote a Node + Express + Backbone error handling system today. I've been staying sane through the use of the 'Q' promise package ( https://github.com/kriskowal/q ). For example, in the below code chunk any errors thrown in makeEmailConfirmation or sendEmailConfirmation will land in the .fail() catch block. I believe using a promise system is the only way to avoid callback hell. var deferred = Q.defer(…

+1 for promises based exception handling. I have used Q for a number of applications. Q really simplifies error handling and by chaining error propagation forward to asynchronous result consumers.
Post reply on HN