[英]Specific Error Catch in BlueBird promise
我正在使用藍鳥承諾和標准錯誤。 問題是當我拋出這樣的錯誤
return new PromiseReturns(function (resolve, reject) {
reject(new StandardError({
status: 'Error',
message: "Not Found",
originalError: err,
code: 404
}));
});
它沒有在這個捕獲中收到
.catch(StandardError , function(err){
})
相反,它收到了
.catch(function(err){
})
這個對我有用。 看一下這個
var Promise = require('bluebird')
var StandardError = require("standard-error")
Promise.resolve().then(function() {
throw new StandardError("Not Found", {code: 404})
}).catch(StandardError, function(e) {
console.log('custom error caught');
}).catch(function(e) {
console.log('generic caught');
})
輸出:
$ custom error caught
每次造成問題時都要創建新的PromiseReturns。 我已將所有代碼捆綁在一個單一的承諾中,並且有效。
例如:
function requestFromController(body){
return new PromiseReturns(function (resolve, reject) {
if(body){
reject(new StandardError({
status: 'Error',
message: "Not Found",
originalError: err,
code: 404
}));
}
db.model.find().then(x => {
resolve(x);
})
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.