Tag: 鲁棒性

如何使用promise在express.js中正确处理错误(string或对象)

我不是在我的第一个express.js应用程序,但我仍然找出最强大的方法来处理错误。 由于io.js是几个月的现实,我使用原生Promise来帮助自己asynchronous,下面的代码反映了这一点。 我的error handling中间件如下: router.use(function (err, req, res, next) { // in case err.custom is present, means is an "handled" Error, created by developers if (!!err.custom) { return res.status(err.code).json(err.message); } if (err instanceof Error) { console.error(err.stack); return res.status(500).send('Runtime Error'); // should be reported! } // last but not least, validation error res.status(400).send(err); }); 一个示例控制器是这样写的: function […]