Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Express js error handling

Tags:

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"); }); 
like image 282
Eric Speelman Avatar asked Mar 28 '13 14:03

Eric Speelman


People also ask

What is error handling in Express JS?

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.

What is error handling in node JS?

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.

How do I use Express Async error?

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.


2 Answers

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"); }); 
like image 61
Ben Evans Avatar answered Sep 27 '22 21:09

Ben Evans


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

like image 23
Eric Elliott Avatar answered Sep 27 '22 21:09

Eric Elliott