BlueBird 承诺中的特定错误捕获
Specific Error Catch in BlueBird promise
我正在使用 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);
})
});
}
我正在使用 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);
})
});
}