簡體   English   中英

Node.js(Express)錯誤處理中間件與路由器

[英]Node.js (Express) error handling middleware with router

這是我的應用程序結構:

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

ExpressJS應用程序為developmentproduction環境創建錯誤處理程序。 這是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: {}
    });
});

routes/index.js里面,我處理所有路由:

var router = express.Router();

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

module.exports = router;

我希望將err傳遞給其中一個錯誤處理程序而不是拋出。 我怎樣才能做到這一點?

您必須將它傳遞給下一個回調,這通常是路由處理程序中的第三個參數

var router = express.Router();

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

module.exports = router;

調用next(err)將允許使用以下簽名在鏈中的中間件中捕獲錯誤:

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

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

您還可以創建中間件函數來處理所有路徑中的錯誤,而無需在任何地方復制代碼,如果您願意,可以使用箭頭函數。

1)創建一個const函數來處理錯誤。

之一:

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

要么

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

2)在你的路由器中使用它來滿足每個請求:

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)在您的服務器主應用程序處理錯誤:

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

這樣,您可以在任何路由中重用錯誤處理功能。 此外,如果您的任何功能中存在任何未處理的錯誤,這也會捕獲它們。

嘗試從Here獲取catch錯誤處理

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM