How to handle promising requests?

So, I'm using request-promise in a script that I have that goes through a list of URLs and requests. Then I want to do something with the data retrieved after all the queries have completed.

I have the following:

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); }); 

As you can see. http://httpstat.us/500 returns 500, which causes the .catch () block to run. I make a mistake. should.throw.error.to.console(); should cause an error on the console, but instead the script just exits without any error codes ( Process finished with exit code 0 ).

I assume that the promise request catches an error from node http when the page does not return with the wxxxxx code and then passes it back to the catch () callback. But any subsequent errors then fail silently. How do I deal with this in the world so that the rest of my code still throws errors correctly?

Related GitHub issue

+5
source share
2 answers

Thus, it is obvious that .catch () in promises is basically a wrapper around try-catch JS. Therefore, in order for subsequent errors to be recorded on the console after one handler has already been written, you must have a second handler to display a possible error on the console.

More information about GitHub here: https://github.com/request/request-promise/issues/48#issuecomment-107734372

0
source

What do you mean by "any subsequent errors and then fail"? If the original rp promise is not fulfilled, catch is executed ... during the failure. Once a promise is rejected, it cannot be "subsequent errors."

In addition, it should look like a statement (e.g. from chai ) that suggests that you are trying to verify this. Chai should.throw does not should.throw error, it checks that an error has been selected. If you are testing this, you need to point to the test ( it block) that the test is asynchronous and not synchronized - usually by assigning a name and calling done . Otherwise, the request will be sent, and then before any response is executed, the script will be synchronously completed and no errors will be listened.

What else, are you speculating that something should throw to start the console, but nothing in your code throw s! If you pointed to throw , you should understand that throw inside then or catch will simply reject the outgoing promise from this handler with the given value (yes, catch export the new promise, just like then - this is 100% sugar for .then(null, errHandler) . If you want errors to be returned back to the window, you need to complete the chain using the promise method Bluebird .done() , accessed by request - ask through several arcane .promise().done() . But even in this case, you will still need to indicate that you are running asynchronous ones Art.

In short, it’s not entirely clear what you think about what this code should do, and how it differs from your expectations. Please clarify!

 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 (eg 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 (eg, 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 complete by invoking `done()` (not the same as ending the promise chain in Bluebird `.done()`). 
+1
source

All Articles