expressjs / express

Fast, unopinionated, minimalist web framework for node.
https://expressjs.com
MIT License
65.72k stars 16.32k forks source link

500 error handler ignored, but exception is caught in process.on('uncaughtException') #1510

Closed binarykitchen closed 11 years ago

binarykitchen commented 11 years ago

Hello guys

I've googled everywhere about my problem without success so I'm trying here to get some help.

In my code, express does not show the 500 error page when an error happens but is logged within process.on('uncaughtException').

Here are the relevant parts of my code:

// near top of server.js
process.on('uncaughtException', function (err) {
    console.error(err);
});

..

// further down in the middle ...
app.configure(function() {
    ...
    app.use(function(err, req, res, next) {
        res.status(err.status || 500);
        res.render('500', { ...}
        );
    });
});

...
require('./routes/clean')(app);

Then somewhere in the route /clean an error is thrown and caught in process.on('uncaughtException') but is never rendered as 500 within express. Why?

tj commented 11 years ago

you have to delegate errors with next(err) in async callbacks, for example:

db.find(id, function(err){
  if (err) throw err;
})

cannot be caught by express, while the following can be:

db.find(id, function(err){
  if (err) return next(err);
})

so something must be throwing instead of delegating

binarykitchen commented 11 years ago

ah, thanks! this is very important information. can you add this somewhere in the guide? http://expressjs.com/guide.html

one last question: what if I'm creating new exceptions inside async callbacks?

like this:

throw new Exception('errrrrror');

should I use

next(new Exception('errrrrror'));

instead?

binarykitchen commented 11 years ago

and um, does this rule apply to all async callbacks or only those within express routes?

for example async callbacks within socket.io?

tj commented 11 years ago

yup it applies to all of node really, one of node's major codesmells

binarykitchen commented 11 years ago

all of node? ouch!

is there an ongoing discussion somewhere else about this smell?