我创建了nodejs + Express应用程序。现在在我的应用程序中,当异常捕获错误时发送如下
app.get('/data', (req, res) => {
if(!req.params.token){
return res.status(403).send('Access token not provided');
}
//do something here
});
Run Code Online (Sandbox Code Playgroud)
而不是发送res.status(403).send('未提供访问令牌'); 我可以发送这样的东西吗
异常.js
class Forbidden {
constructor(message,stack = null){
this.code = 403;
this.message = message
this.stack = stack;
}
}
Run Code Online (Sandbox Code Playgroud)
应用程序.js
var httpForbidden = require('exception.js');
app.get('/data', (req, res) => {
if(!req.params.token){
return new httpForbidden ('Access token not provided');
}
//do something here
});
Run Code Online (Sandbox Code Playgroud)
我怎样才能在一个地方捕获所有异常?