Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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

how to handle all error of all router in express

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;
Comment

how to handle all error of all router in express

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

PREVIOUS NEXT
Code Example
Javascript :: jquery get radio checked value 
Javascript :: how to add background image in mui 
Javascript :: lexical scoping javascript 
Javascript :: how to change the color of error message in jquery validation 
Javascript :: async in useeffect 
Javascript :: react useeffect 
Javascript :: a function that calls itself js 
Javascript :: npm yarn run shell script 
Javascript :: js array fill map 
Javascript :: Find smallest Number from array in javascript 
Javascript :: check value exist in array javascript 
Javascript :: cancel button in react js 
Javascript :: json to list flutter 
Javascript :: submit form in vue 
Javascript :: ionic cordova icon notification 
Javascript :: p5js cdn 
Javascript :: react native text get number of lines 
Javascript :: link button material ui 
Javascript :: mongodb update many 
Javascript :: nested for loop javascript 
Javascript :: convert elements to array javascript 
Javascript :: how to add two attay into object in javascript 
Javascript :: javascript title string 
Javascript :: how to reset node command prompt 
Javascript :: express redirect 
Javascript :: wait for ajax to finish 
Javascript :: js onchange input value event listene 
Javascript :: string contains character javascript 
Javascript :: discord.js button 
Javascript :: get offset from timezone javascript 
ADD CONTENT
Topic
Content
Source link
Name
1+2 =