Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

626
Visualizações
Node.js (Express) error handling middleware with router

Here's my application structure:

- app.js
- routes
---- index.js

The ExpressJS app creates error handlers for development and production environments. Here's a code snippet from app.js:

app.use('/', routes); // routing is handled by index.js in the routes folder

//The following middleware are generated when you create the Express App

// catch 404 and forward to error handler
app.use(function (req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
    app.use(function (err, req, res, next) {
        res.status(err.status || 500);
        res.render('error.ejs', {
            message: err.message,
            error: err
        });
    });
}

// production error handler
// no stacktraces leaked to user
app.use(function (err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
        message: err.message,
        error: {}
    });
});

And inside of routes/index.js, where I handle all the routing:

var router = express.Router();

router.get('/', function (req, res) {
    someAsyncFunction(function(err, result) {
        if (err) throw err; // Handle this error
    }
});

module.exports = router;

I want the err to be passed to one of the error handlers instead of being thrown. How can I do this?

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You have to pass it to the next callback which is usually the third parameter in the route handler

var router = express.Router();

router.get('/', function (req, res, next) {
    someAsyncFunction(function(err, result) {
        if (err) {
            next(err); // Handle this error
        }
    }
});

module.exports = router;

calling next(err) will allow the error to be caught in a middleware down the chain with the following signature:

app.use(function (err, req, res, next){
    // do something about the err
});

Reference: http://expressjs.com/en/guide/error-handling.html

about 4 years ago · Santiago Trujillo Relatório

0

You can also create a middleware function to handle error in all routes without copying code everywhere, using arrow functions if you like.

1) Create a const function to handle errors.

either:

const handleErrorAsync = func => (req, res, next) => {
    func(req, res, next).catch((error) => next(error));
};

or

const handleErrorAsync = func => async (req, res, next) => {
    try {
        await func(req, res, next);
    } catch (error) {
        next(error);
    }
};

2) In your router use it for every request:

var router = express.Router();

router.get('/req1', handleErrorAsync(async (req, res, next) => {
   let result = await someAsyncFunction1();
   if(result){
       // res.send whatever
   }
}));
router.post('/req2', handleErrorAsync(async (req, res, next) => {
    let result = await someAsyncFunction2(req.body.param1);
    if(result){
        // res.send whatever
    }
}));
router.post('/req3', handleErrorAsync(async (req, res, next) => {
    let result = await someAsyncFunction3(req.body.param1, req.body.param2);
    if(result){
        // res.send whatever
    }
}));

module.exports = router;

3) In your server main app handle error:

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
    app.use(function (err, req, res, next) {
        res.status(err.status || 500);
        res.render('error.ejs', {
            message: err.message,
            error: err
        });
    });
}

// production error handler
// no stacktraces leaked to user
app.use(function (err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
        message: err.message,
        error: {}
    });
});

This way you can reuse the error handling function in any route. Also, if there are any unhandled errors in any of your functions, this will catch them as well.

Try catch error handling taken from Here

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda