请求承诺无法传播错误
request-promise unable to propagate error
这是我的 rp 电话:
function hit() = {
return rp(options).then((res) => {
return res;
}).catch((err) => {
console.log("Error occured");
throw err;
})
}
和调用此函数的函数:
hit().then( res => {
console.log(ae_res)
}).error( err => {
console.err(err) /// code never hits this line
});
当我 运行 它时,如果 rp 调用成功,则一切正常。但如果它失败了,我永远不会在被调用函数中点击 catch() 块。
我无法传播 rp 调用抛出的原始错误。
您正在使用 .error
而不是 .catch
。
根据 Bluebird manual (which is the type of Promise that request-promise returns), .error
only handles OperationalErrors,这基本上是在不使用 throw 的情况下拒绝 Promise 的错误。对于抛出的错误,您必须使用 .catch
:
hit().then( res => {
console.log(ae_res)
}).catch( err => {
console.err(err)
});
这是我的 rp 电话:
function hit() = {
return rp(options).then((res) => {
return res;
}).catch((err) => {
console.log("Error occured");
throw err;
})
}
和调用此函数的函数:
hit().then( res => {
console.log(ae_res)
}).error( err => {
console.err(err) /// code never hits this line
});
当我 运行 它时,如果 rp 调用成功,则一切正常。但如果它失败了,我永远不会在被调用函数中点击 catch() 块。
我无法传播 rp 调用抛出的原始错误。
您正在使用 .error
而不是 .catch
。
根据 Bluebird manual (which is the type of Promise that request-promise returns), .error
only handles OperationalErrors,这基本上是在不使用 throw 的情况下拒绝 Promise 的错误。对于抛出的错误,您必须使用 .catch
:
hit().then( res => {
console.log(ae_res)
}).catch( err => {
console.err(err)
});