UncaughtException in Express and Resify

I use both options: reify and express.

In the update, I create the server this way:

server = restify.createServer(serverSettings); 

Then I can handle uncaughtException as follows:

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

This is different from process.on ('uncaughtException'). Because he caught all uncaughtException and can give an answer to the client. So I like this way to catch the exception.

But in Express, I cannot find something like this.

So I just want to ask if there is the same thing on the express? or can i implement the same function?

+4
source share
1 answer

Update 2014: In retrospect, NodeJS domains are unstable and bizarre. They produce a lot of regional affairs and are not very funny. Promises is probably the best option for handling errors - good promise libraries like Bluebird and Q create good stack traces and Bluebird fast - Promises also have a catch security guarantee.

Update 2017: you definitely need to use async functions for any asynchronous operations that you do, this one time and use this to handle exceptions and use the language built into tools like asynchronous iterators to handle exceptions. By now, the days of writing the callback suffix are long gone.


Use domains .

Domains were introduced in version 0.8 and work, so they are quite new. At 0.10 they are pretty stable. They provide a preferred approach to the uncaughtException event. The awesome domains are obsolete: D. This is not specific to an expression or any other specific structure or library.

In general, domains allow code separation. Everything that you start in the domain will allow the domain to catch it. They allow you to get brief stack traces and tell the server what to do when an error occurs. You can even use domains for specific parts of the server, but keep in mind that exceptions are relatively expensive 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 also wrote a simple try / catch for asynchronous exceptions here if you find this interesting :)

+4
source

Source: https://habr.com/ru/post/1494212/


All Articles