2

I have the following code:

return requestAsync({
     method: 'GET',
     url: 'https://' + servers[num - 1] + ':8033/version.txt'
}).then().catch()

I tried throwing an error in the then handler but that didn't work

If a condition is not met in the then handler, I want throw an error that the catch handler handles. How can I get that done?

Code:

var P = require('bluebird');
var defer = function () {
    var resolve, reject;
    var promise = new P(function () {
        resolve = arguments[0];
        reject = arguments[1];
    });
    return {
        resolve: function () {
            resolve.apply(null, arguments);
            return promise;
        },
        reject: function () {
            reject.apply(null, arguments);
            return promise;
        },
        promise: promise
    };
};

var pool = {maxSockets: Infinity};
var requestAsync = function (options) {
    options.pool = pool;
    options.timeout = 60000;
    options.rejectUnauthorized = false;
    options.strictSSL = false;
    var deferred = defer();
    var r = request(options, function (err, res, body) {
        if (err) {
            return deferred.reject(err);
        }
        deferred.resolve(res, body);
    });

    deferred.promise.req = r;

    return deferred.promise;
};

return requestAsync({
     method: 'GET',
     url: 'https://' + servers[num - 1] + ':8033/version.txt'
}).then(function (response) {
     throw new Error('Server is not taken');
}).catch(function (err) { });
Brown KL
  • 2,283
  • 4
  • 14
  • 18
  • Please show us your actual code. Where are you throwing an error? You're not even passing handlers to `then` and `catch`! – Bergi Mar 10 '16 at 02:27
  • What is `requestAsync`, which promise library are you using? – Bergi Mar 10 '16 at 02:28
  • I'm using bluebird node js library – Brown KL Mar 10 '16 at 02:35
  • Bluebird is fine normally. So what exactly is your problem, what does not work? – Bergi Mar 10 '16 at 02:37
  • I throw an error in the then() but the catch doesn't get it – Brown KL Mar 10 '16 at 02:43
  • 1
    **Show us that code!** We can't help you otherwise. If you were doing it right, it would work. – Bergi Mar 10 '16 at 02:44
  • I'd recommend to [avoid the deferred pattern](http://stackoverflow.com/a/28692824/1048572). Also notice that `resolve` only takes one argument, not multiple. Apart from that, your code seems about fine. Now what does not work? Do you see the `then` callback execute, but the `catch` callback not? Please put in some logging, and show us the output. – Bergi Mar 10 '16 at 02:52
  • Eeeck, why all the code for `defer()`. That function is not needed here at all. Just use the promise that `requestAsync()` already returns. Learn how to use promises correctly and you will have far fewer problems. – jfriend00 Mar 10 '16 at 03:04
  • @jfriend00: The code with `defer` is *inside* `requestAsync` :-) – Bergi Mar 10 '16 at 05:25
  • @Bergi - I know. I'm saying there's no reason at all to use it. I would have thought you of all people would agree with that sentiment. – jfriend00 Mar 10 '16 at 05:31
  • @jfriend00: A custom promisification (instead of `var requestAsync = P.promisify(reqest)`) seems to be necessary here because he wants to have a `.req` property on his promise. Of course - as I already commented - he rather should use the `Promise` constructor for this rather than a deferred. – Bergi Mar 10 '16 at 05:36

2 Answers2

6

You can manually throw the error:

requestAsync({
 method: 'GET',
 url: 'https://' + servers[num - 1] + ':8033/version.txt'
})
.then(function () { 
  throw new Error("Catch me")
}))
.catch(function (error) {
  console.error(error)
})

jsbin: https://jsbin.com/dewiqafaca/edit?html,js,console,output

agconti
  • 17,780
  • 15
  • 80
  • 114
0

Just use throw to generate a standard JavaScript exception in your then function and it should invoke the function in your catch block with whatever value you provide as the argument.

kungphu
  • 4,592
  • 3
  • 28
  • 37