[英]Overwrite default error messages of loopback
我正在嘗試覆蓋環回的默認錯誤消息。 這就是我的做法:
server / middleware.json:
{
"initial:before": {
"loopback#favicon": {}
},
"initial": {
"compression": {},
"cors": {
"params": {
"origin": true,
"credentials": true,
"maxAge": 86400
}
},
"helmet#xssFilter": {},
"helmet#frameguard": {
"params": [
"deny"
]
},
"helmet#hsts": {
"params": {
"maxAge": 0,
"includeSubdomains": true
}
},
"helmet#hidePoweredBy": {},
"helmet#ieNoOpen": {},
"helmet#noSniff": {},
"helmet#noCache": {
"enabled": false
}
},
"session": {},
"auth": {},
"parse": {},
"routes": {
"loopback#rest": {
"paths": [
"${restApiRoot}"
]
}
},
"files": {},
"final": {
"loopback#urlNotFound": {}
},
"final:after": {
"loopback#errorHandler": {},
"errorHandler": {}
}
}
服務器/中間件/error-handler.js:
module.exports = (error, req, res, next) => {
console.log('a')
};
在覆蓋之前,我需要首先攔截錯誤消息,但是我不知道如何...
謝謝!
您可以訪問錯誤消息並按如下所示覆蓋它
module.exports = (error, req, res, next) => {
console.log(error.message); // logs out original error message
error.message = 'Your Custom Error message'; // this will overwrite the error message
next(error); // this is important
};
錯誤對象的其他屬性是
statusCode, name, stack & also details, messages if applicable
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.