【问题标题】:request-promise unable to propagate errorrequest-promise 无法传播错误
【发布时间】:2018-06-25 23:19:51
【问题描述】:

这是我的 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 调用引发的原始错误。

【问题讨论】:

  • 请记住,404 或 500 错误不会构成被拒绝的承诺。联系服务器并获得答复(任何答复)被视为成功请求,即使答复是 4xx 或 5xx 状态。因此,4xx 或 5xx 状态结果仍将进入then() 处理程序。

标签: node.js ecmascript-6 promise request-promise


【解决方案1】:

您使用的是.error 而不是.catch

根据Bluebird manual(这是request-promise 返回的Promise 类型),.error 仅处理OperationalErrors,这基本上是拒绝Promise 而不使用throw 的错误。对于抛出的错误,您必须使用.catch:

hit().then( res => {
    console.log(ae_res)
}).catch( err => {
    console.err(err)
});

【讨论】:

    猜你喜欢
    • 2016-03-13
    • 1970-01-01
    • 2020-08-19
    • 1970-01-01
    • 2017-11-10
    • 1970-01-01
    • 1970-01-01
    • 2017-06-25
    相关资源
    最近更新 更多