exception handling middleware

50 views
Skip to first unread message

Reza Razavipour

unread,
Nov 13, 2014, 12:32:12 PM11/13/14
to nod...@googlegroups.com
I want to have an application global exception handler. 
I have added 

app.use(function(err, req, res, next) {
logger.error(err.message);
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});

and in my code, to test this, I throw something like 
throw new Error('catch me');

and I never see my middleware catching it.

Thoughts?

Reza

Ryan Schmidt

unread,
Nov 13, 2014, 9:15:11 PM11/13/14
to nod...@googlegroups.com
If you're using express version 4 or later, make sure you app.use your error handler *after* you've defined all your normal routes. Other than that, I'm not sure; show us your code.

Jimb Esser

unread,
Nov 17, 2014, 4:10:31 PM11/17/14
to nod...@googlegroups.com
That will only catch exceptions thrown by synchronous middleware/handlers, if you're doing anything asynchronous, Express is not going to catch it.  Try express-domain-middleware to use domains to catch all exceptions relative to a request and have them passed to your Express error handler.
Reply all
Reply to author
Forward
0 new messages