6

I use both restify and express.

In restify, I create server in this way:

server = restify.createServer(serverSettings);

Then, I can handle uncaughtException like this:

server.on('uncaughtException', function(req, res, route, err) {})

It's different with process.on('uncaughtException'). Because it's caught all uncaughtException and can give a response to client. So I like this way to catch the exception.

But in Express, I can't find something like this.

So just want to ask, is there a same thing in express? or there are some way I can implement the same function?

Zhe
  • 1,060
  • 1
  • 11
  • 19

1 Answers1

4

Update 2014: In retrospect, NodeJS domains are kind of unstable and quirky. They produce a lot of edge cases and are not a lot of fun. Promises are likely a better option for error handling - good promise libraries like Bluebird and Q produce good stack traces and Bluebird is fast - promises also have a catch safety guarantee.

Update 2017: You should definitely be using async functions for anything asynchronous you're doing that is one time and use that for exception handling and use language built in tools like async iterators for exception handling. The days of writing callback soup are long gone by now.


Use domains.

Domains were introduced in version 0.8 and are being worked on so they're pretty new. In 0.10 they're pretty steady. They provide a preferably approach to a "uncaughtException" event. Domains are awesome deprecated :D. It's not specific to express, or any other specific framework or library.

Generally speaking, domains lets you do code separation. Everything you start in a domain will let the domain catch it. They let you get concise stack traces and tell the server what to do when an error occurs. You can even use domains for specific parts in the server although keep in mind exceptions are relatively expensive things in the JS world.

var d = domain.create();
d.on('error', function(er) {
   //your handler here    
});
d.run(function(){
    //create the server here, 
    //errors thrown will be handled by the domain handler
});

I've also written a simple try/catch for asynchronous exceptions here in case you find that interesting :)

Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
  • 1
    Thanks Benjamin. But from you example, I can see it's a good way to catch all errors. But my question is how to give a correct response to client. In your example, the error handler function just have a err, the response is lost, which mean I just catch the error, but can't give a response. – Zhe Jul 30 '13 at 08:05
  • @Zhe If you check the page I linked you to (domains) they specifically show how you can add the request and response to a domain and address errors like you want. See the section titled 'explicit binding'. – Benjamin Gruenbaum Jul 30 '13 at 08:12
  • 1
    You [_could_](http://expressjs.com/guide.html#error-handling) use `"errorHandler`" in express (app.use with a function) but it's not nearly as tight as domains are. – Benjamin Gruenbaum Jul 30 '13 at 08:20
  • 1
    Thanks @Benjamin, I just go through the domain in nodejs doc. It's really a good way to process the error. I also check the restify code to see how they do the 'uncaughtException' things I mention in my question. And I found that they do use the domain to apply this. So actually, I already use it :). Thanks a lot for your help. – Zhe Aug 01 '13 at 01:54
  • @Zhe Props for going through the source code - that's always the best way to deal with how to handle this sort of stuff. – Benjamin Gruenbaum Aug 01 '13 at 05:37
  • Take caution , Domain is deprecated . – Shailesh kumar Jul 06 '17 at 09:29