繁体   English   中英

如何正确处理请求承诺错误?

[英]How to handle request-promise errors properly?

所以我在一个脚本中使用了request-promise ,这个脚本循环遍历url和请求的激活列表。 然后,我希望在所有请求完成后,接收数据。

我有以下内容:

var rp = require('request-promise');

rp.get({
    uri: 'http://httpstat.us/500',
    transform: function(body, res){
        res.data = JSON.parse(body);
        return res;
    }
}).then(function(res){
    results.push(res.data);
})
.catch(function(err){
    should.throw.error.to.console();
    var respErr  = JSON.parse(err.error);
    var errorResult = {
        origUrl: respErr.origUrl,
        error: respErr
    };
    results.push(errorResult);
});

正如你所看到的.. http://httpstat.us/500抛出一个500,这将导致.catch()块中跑去。 我在逼错。 should.throw.error.to.console(); 应该向控制台抛出一个错误,但是,脚本只是静默退出没有任何错误代码( Process finished with exit code 0 )。

我假设当一个页面没有返回w / 2xx代码然后将其传递回catch()回调时,请求承诺正在从节点的http捕获错误。 但任何后续错误最终都会以无声的方式失败。 我如何处理这个问题,以便我的其余代码仍能正确抛出错误?

相关的GitHub 问题

什么是“任何后续错误然后最终失败”的意思是什么? 如果原始promise rp失败,则catch 失败时执行.... 一旦承诺被拒绝,就是这样,就不会有“后续错误”。

此外, should看起来像一个断言(例如从chai ),这表明你试图测试这个。 Chai的should.throw不会抛出错误,它会检查是否抛出了错误。 如果您正在测试它,您需要指示测试( it阻止)测试是异步的,而不是同步 - 通常是通过命名和调用done参数。 否则,将发出请求,然后在可以做出任何响应之前,脚本将同步结束并且不会收听任何错误。

更重要的是,你正在指出某些东西应该throw控制台,但你的代码中没有任何东西throw 如果你没有在写throw ,你应该明白, throw里面thencatch将会使得从处理器传出的承诺与抛出值(是的,拒绝catch出口新的承诺,就像then -这是100 .then(null, errHandler) %糖。如果你想将错误重新抛回窗口,你需要使用Bluebird的.done() promise方法完成链,通过有点神秘的方式在request-promise中访问.promise().done() 。但即使在这种情况下,您仍然需要指定您正在进行异步测试。

简而言之,您认为某些代码应该在做什么以及它与您的期望有何不同并不完全清楚。 请澄清!

var rp = require('request-promise');

rp.get({ // start an async call and return a promise
    uri: 'http://httpstat.us/500',
    transform: function(body, res){
        res.data = JSON.parse(body);
        return res;
    }
}).then(function(res){ // if rp.get resolves, push res.data
    results.push(res.data);
})
.catch(function(err){ // if rp.get rejects (e.g. 500), do this:
    should.throw.error.to.console(); // test if something is thrown (but nothing has been!)
    var respErr  = JSON.parse(err.error);
    var errorResult = {
        origUrl: respErr.origUrl,
        error: respErr
    };
    results.push(errorResult); // push an object with some of the error info into results
});

// this line (e.g., end of script) is reached before any of the async stuff above settles. If you are testing something, you need to make the test async and specify when it's complete by invoking `done()` (not the same as ending the promise chain in Bluebird's `.done()`).

很明显,promises中的.catch()基本上是JS try-catch的包装器。 因此,为了在已经编写了一个处理程序后将后续错误记录到控制台,您必须有第二个处理程序将最终错误抛出到控制台。

有关GitHub的更多信息,请访问: https//github.com/request/request-promise/issues/48#issuecomment-107734372

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM