5 ms·
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 http://ratchet
by coryvirok 14y ago
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 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 that you can instantly recognize as missing if someone forgets to check for err in the callback.
If you can stick with the style, it's fairly difficult to write code that doesn't deal with errors gracefully.
- STRML 14y agoI agree. One of the advantages of the (err, result) style is that it puts error handling directly in your face. The `result typeof Error` style does not. In any case all I care about is that we choose one, and (err, result) is gaining traction, which is fine. While many are excited about domains, I am not. From my perspective, in practice, it will just add another incompatible style onto the pile.
- mcantelon 14y agoYeah, domains seem like a desperate solution. Will be interesting to see what eventually becomes the standard.
- kevingadd 14y agoHow do you deal with code where the author forgot to handle all of the failure cases and invoke the callback?
- STRML 14y agoTo be honest... since you can't try/catch, you can't do much. You can catch the exception with something like this: process.on('uncaughtException', function (err) { console.err("Uncaught exception: " + err); console.err("Uncaught exception stack: " + err.stack); }); I use the built-in clustering module (implementation is very simple) so a simple worker crash will result in the error being logged & the worker being restarted. That's about the best you can do, unfortunately. You could always monkey patch the lib if you can figure out where it's crashing. I recommend Longjohn(https://github.com/mattinsler/longjohn https://github.com/mattinsler/longjohn) for these sorts of situations; the extra stack trace lines become invaluable.
- jahewson 14y agoFix the code.