how to handle all error of all router in express

// 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: {}
    });
});

3
1
Hanno Fietz 120 points

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

3 (1 Votes)
0
3
3
Simon Crase 125 points

                                    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 (3 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
error route express how to trigger an error in an express route best way to handle errors express router node show error on all routes throw error controller route express return error controller route express define error middleware express err express funciton error handling middleware error handling express howt o handle route errors in nodejd error handling in node js express how to handel error in express nod js nodejs throw new Error(msg) middleware node js express global error handler throw exception from route js express controller then catch node route common error catcher how to catch thrown error in promise express catch erros in express js app level error handling typescript express global exception handling in expressjs how to handle errors in express js Express.Application error unable to load global error handler express express js 404 page xpress error handler status c ode xpress error handler code express error handler code err object express express global error handler res.render catch send error status express error handling express js express filter exception? get status error text express how to send error in response express express nodejs error app.use("/",middlwarre) shows an error best way to handle errors in express express js send error response throw error express not wrking how to send error in express express render error express handle errors in routes error handling helpers express http status code for trycatch user login express og all application-level errors javascript express express js error try catch middleware express catch error in express post route express bad request how to get error message in catch block express express 2020 error handler app.use((err req res next) express catch global eceptions error handling express app how to handle error in js while using express how to accses to Error masage in express express send error response express .get error message error handling middleware express express app.use error handler express router get error handling express custom error express js error handling err express how to throw an error in express express custom error handler express route error handling express api try catch route error express express handle errors in route express handle erros error method in express OAuth Error Exception handling in node route how to handle all error of all router in express
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source