I'm trying to get error handling running with express but instead of seeing a response of "error!!!" like I expect I see "some exception" on the console and then the process is killed. Is this how error handing is supposed to be setup and if so is there another way to catch errors?
var express = require('express'); var app = express(); app.use(function(err, req, res, next) { console.log("error!!!"); res.send("error!!!"); }); app.get('/', function(request, response) { throw "some exception"; response.send('Hello World!'); }); app.listen(5000, function() { console.log("Listening on 5000"); });
Error Handling refers to how Express catches and processes errors that occur both synchronously and asynchronously. Express comes with a default error handler so you don't need to write your own to get started.
The exception handling refers to the mechanism by which the exceptions occurring in a code while an application is running is handled. Node. js supports several mechanisms for propagating and handling errors.
To handle an error in an asynchronous function, you need to catch the error first. You can do this with try/catch . Next, you pass the error into an Express error handler with the next argument. If you did not write a custom error handler yet, Express will handle the error for you with its default error handler.
An example app/guide on error handling is available at https://expressjs.com/en/guide/error-handling.html However should fix your code:
// Require Dependencies var express = require('express'); var app = express(); // Middleware app.use(app.router); // you need this line so the .get etc. routes are run and if an error within, then the error is parsed to the next middleware (your error reporter) app.use(function(err, req, res, next) { if(!err) return next(); // you also need this line console.log("error!!!"); res.send("error!!!"); }); // Routes app.get('/', function(request, response) { throw "some exception"; response.send('Hello World!'); }); // Listen app.listen(5000, function() { console.log("Listening on 5000"); });
A few tips:
1) Your code wasn't working because your error handler middleware was run before your route was reached, so the error handler never had a chance to have the error passed to it. This style is known as continuation passing. Put your error handler last in the middleware stack.
2) You should shut down the server when you have an unhandled error. The best way to do that is to call server.close()
, where server is the result of doing var server = http.createServer(app);
Which means, you should do something like this:
var server = http.createServer(app); app.use(function(err, req, res, next) { console.log("error!!!"); res.send("error!!!"); server.close(); });
You should probably also time out the server.close(), in case it can't complete (your app is in an undefined state, after all):
var server = http.createServer(app); app.use(function(err, req, res, next) { console.log("error!!!"); res.send("error!!!"); server.close(); setTimeout(function () { process.exit(1); }, 3*1000); });
I made a library that does all this for you, and lets you define custom responses, including specialized error views, static files to serve, etc...:
https://github.com/ericelliott/express-error-handler
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With