Home > Software design >  Express.js: Express global error handler not working, exception still crash the application
Express.js: Express global error handler not working, exception still crash the application

Time:08-31

I added express global error handler at the last few lines of app.js, however exception throw in controller still crash the application.

identity.controller.js

const createError = require('http-errors');
exports.identifyUser = async (req, res) => {
    throw new createError(404, 'Test');
}

app.js

// Configure routes
routes.register(app);

app.use(function(err, req, res, next) {
    res.status(err.status || 500).json(response.error(err.status || 500));
});

module.exports = app;

When identifyUser was called, I except a 500 response from express. However, an exception was uncatched and kill my application.

NotFoundError: Test
    at exports.identifyUser (C:\Users\......\src\api\controllers\identity.controller.js:9:11)
    at Layer.handle [as handle_request] (C:\Users\......\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Users\......\node_modules\express\lib\router\route.js:144:13)
    at C:\Users\......\src\api\middlewares\authUser.js:30:16
    at C:\Users\......\node_modules\jsonwebtoken\verify.js:223:12
    at getSecret (C:\Users\......\node_modules\jsonwebtoken\verify.js:90:14)
    at module.exports [as verify] (C:\Users\......\node_modules\jsonwebtoken\verify.js:94:10)
    at C:\Users\......\src\api\middlewares\authUser.js:19:16
    at Layer.handle [as handle_request] (C:\Users\......\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Users\......\node_modules\express\lib\router\route.js:144:13)
    at Route.dispatch (C:\Users\......\node_modules\express\lib\router\route.js:114:3)
    at Layer.handle [as handle_request] (C:\Users\......\node_modules\express\lib\router\layer.js:95:5)
    at C:\Users\......\node_modules\express\lib\router\index.js:284:15
    at Function.process_params (C:\Users\......\node_modules\express\lib\router\index.js:346:12)
    at next (C:\Users\......\node_modules\express\lib\router\index.js:280:10)
    at Function.handle (C:\Users\......\node_modules\express\lib\router\index.js:175:3)
    at router (C:\Users\......\node_modules\express\lib\router\index.js:47:12)
    at Layer.handle [as handle_request] (C:\Users\......\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (C:\Users\......\node_modules\express\lib\router\index.js:328:13)
    at C:\Users\......\node_modules\express\lib\router\index.js:286:9
    at Function.process_params (C:\Users\......\node_modules\express\lib\router\index.js:346:12)
    at next (C:\Users\......\node_modules\express\lib\router\index.js:280:10)

Node.js v18.7.0
[nodemon] app crashed - waiting for file changes before starting...

I'm running Node.js v18.7.0 with Express 4.18.1 on Windows 11.

CodePudding user response:

From the Express documentation:

For errors returned from asynchronous functions invoked by route handlers and middleware, you must pass them to the next() function, where Express will catch and process them.

Try replace

exports.identifyUser = async (req, res) => {
    throw new createError(404, 'Test');
}

by

exports.identifyUser = async (req, res, next) => {
    next(new createError(404, 'Test'));
}

CodePudding user response:

The global error handler from Express will not catch any error in your application, you still have to do it yourself with a try/catch block then call the next function with your error as parameter.

const createError = require('http-errors');

exports.identifyUser = async (req, res, next) => {
  try {
    throw new createError(404, 'Test');
  } catch(err) {
    next(err);
  }
}
  • Related