Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

86
Vistas
Express.js: Express global error handler not working, exception still crash the application

I added an express global error handler at the last few lines of app.js, however a thrown exception in a controller still crashed 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 expect an error response 500 from express. However, an exception was uncaught and killed 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.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

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'));
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

I don't think you need http-errors as long as you're using express, you can just throw new Error.

Try This:

exports.identifyUser = async (request, response, next) => {
    try {
        throw new Error (404, 'Test'));
    } catch {
        next (Error);
    }
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

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);
  }
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda