At the very end of my server.js file I have the following code:
app.use(logErrors);
function logErrors (err: Error, req: Request, res: Response, next: NextFunction) {
console.log(err);
mongoDal.log(err.message, err);
next(err);
}
but this code doesn't get reached when an error occurs.
I also have the Node.js error handling function, which doesn't get reached either:
process.on('uncaughtException', function (err: Error) {
try {
console.log(err);
mongoDal.log(err.message, err);
} catch (err) {
}
});
This is the code that produces the error:
app.get('/test_feature', function (req: Request, res: Response) {
makeError();
res.send("Done");
});
function makeError(){
throw new Error("asdasdad");
}
BTW the error does get written into the console (but not by a function of mine), and the app doesn't crash.
What I'm trying to make is a generic solution that will catch every unhandled exception in one place.
What am I doing wrong?
At the very end of my server.js file I have the following code:
app.use(logErrors);
function logErrors (err: Error, req: Request, res: Response, next: NextFunction) {
console.log(err);
mongoDal.log(err.message, err);
next(err);
}
but this code doesn't get reached when an error occurs.
I also have the Node.js error handling function, which doesn't get reached either:
process.on('uncaughtException', function (err: Error) {
try {
console.log(err);
mongoDal.log(err.message, err);
} catch (err) {
}
});
This is the code that produces the error:
app.get('/test_feature', function (req: Request, res: Response) {
makeError();
res.send("Done");
});
function makeError(){
throw new Error("asdasdad");
}
BTW the error does get written into the console (but not by a function of mine), and the app doesn't crash.
What I'm trying to make is a generic solution that will catch every unhandled exception in one place.
What am I doing wrong?
Share Improve this question edited Nov 24, 2016 at 10:21 Alon asked Nov 23, 2016 at 19:24 AlonAlon 11.9k28 gold badges103 silver badges172 bronze badges 7- How are you defining yout requests?? app.get(req, res)??? – Lucas Katayama Commented Nov 23, 2016 at 20:02
- @LucasKatayama yes – Alon Commented Nov 24, 2016 at 9:52
-
1
Express already has a default error handler, which is probably the one logging the error. My guess is that you're installing your error handler at a point where it's not the very last middleware, but I can't say for sure without knowing how your app is structured (is
server.js
the app entry point, or do you start the app through some other file?) – robertklep Commented Nov 27, 2016 at 13:04 - @robertklep I start my app like that: "node server.js", this is the first file that gets executed. – Alon Commented Nov 27, 2016 at 19:19
-
@Alon hmm, that sounds right. I'm fairly certain that for some reason, your error handler isn't being registered properly and the default error handler kicks in. You could try and run your app in debug mode to see if that sheds any light on what's happening:
DEBUG=* node server.js
– robertklep Commented Nov 27, 2016 at 19:24
4 Answers
Reset to default 10 +100Below is the short working example with handling for 3 types of errors:
1) passed to next()
handler,
2) throw-ed inside route handler,
3) unhandled error inside callback of some function called from route handler.
(1) and (2) are captured using custom error middleware handler (A) and (3) is captured by uncaughtException
handler (B).
Express has own error handler which outputs error if it gets the control using the chain of next()
calls (i.e. if there is no custom error handler or if it passes the control further using next(err, req, res, next)
). That's why you still getting an error message in console even if youyr handler is not triggers.
If you try to run the example for cases (1) and (2), you'll see the error outputs twice - by custom handler (A) and by default Express error handler.
'use strict';
var express = require('express');
var app = express();
var server = app.listen(8080, function() {
console.log('* Server listening at ' + server.address().address + ':' + server.address().port);
});
// Example 1: Pass error in route handler to next() handler
app.use('/1', function(req, res, next) {
console.log('* route 1');
next(new Error('* route 1 error'));
});
// Example 2: throw the error in route handler
app.use('/2', function(req, res, next) {
console.log('* route 2');
throw new Error('route 2 error');
});
// Example 3: unhandled error inside some callback function
app.use('/3', function(req, res, next) {
console.log('* route 3');
setTimeout(function(){
throw new Error('route 3 error');
}, 500);
});
// Error handler A: Express Error middleware
app.use(function(err, req, res, next) {
console.log('**************************');
console.log('* [Error middleware]: err:', err);
console.log('**************************');
next(err);
});
// Error handler B: Node's uncaughtException handler
process.on('uncaughtException', function (err) {
console.log('**************************');
console.log('* [process.on(uncaughtException)]: err:', err);
console.log('**************************');
});
Node version: v7.2.0
Typical error is to place error handlers before route definitions, but according to your description that's not the case.
To locate the issue you can try to reduce your own code to same size as mine and I think, the problem will bee obvious.
UPDATE
Moreover, if I try to run the code you provided (with trivial modifications), it works for me:
'use strict';
var express = require('express');
var app = express();
var server = app.listen(8080, function() {
console.log('* Server listening at ' + server.address().address + ':' + server.address().port);
});
//process.on('uncaughtException', function (err: Error) {
process.on('uncaughtException', function (err) {
try {
console.log('*** uncaughtException:', err);
//mongoDal.log(err.message, err);
} catch (err) {
}
});
//app.get('/test_feature', function (req: Request, res: Response) {
app.get('/test_feature', function (req, res) {
makeError();
res.send("Done");
});
function makeError(){
throw new Error("asdasdad");
}
app.use(logErrors);
//function logErrors (err: Error, req: Request, res: Response, next: NextFunction) {
function logErrors (err, req, res, next) {
console.log('*** logErrors:', err);
//mongoDal.log(err.message, err);
next(err);
}
The result is (stack traces are truncated):
* Server listening at :::8080
*** logErrors: Error: asdasdad
at makeError (/home/alykoshin/sync/al-projects/dev/nmotw/400-express-error-handling/main-stackoverflow.js:32:9)
...........
Error: asdasdad
at makeError (/home/alykoshin/sync/al-projects/dev/nmotw/400-express-error-handling/main-stackoverflow.js:32:9)
...........
You may see at the beginning the ouput of logErrors
handler and then the output of default Express error handler.
Note: your error handler middleware MUST have 4 parameters: error, req, res, next. Otherwise your handler won't fire.
I was fighting this problem until I've discovered this.
I think the problem is on the routing definitions
As you said you are using this
app.get('/test_feature', function (req, res) {
throw new Error("asdasdad");
res.send("Done");
});
Try using this:
app.get('/test_feature', function (req, res, next) {
next(new Error("asdasdad"));
});
And then put res.send on the error handler...
Why?
I think throwing a Error inside the function stops the request chain... So it doesn't reach the very ending and then your error handler. If you write as the second way... you move the error forward ... and then reaches your handler...
If you want the global uncaughtException
to run, you need to throw an error at a place where no one is catching, like this:
app.get('/test_feature', function (req: Request, res: Response) {
setTimeout(function () { throw new Error('oops'); }, 1000);
res.send("Done");
});
A synchronous throw
inside a handler is caught by express, no uncaughtException
happens at all as it was caught.
And according to http://expressjs./en/guide/error-handling.html, an error-handling middleware is defined after your routes and other middlewares.
app.get('/test_feature', function (req: Request, res: Response) {
makeError();
res.send("Done");
});
app.use(logErrors)
If you get the wrong order, there would be no error handler to be called when something is thrown.