request / request-promise-native

The simplified HTTP request client 'request' with Promise support. Powered by native ES6 promises.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Targeted catch blocks

apichick opened this issue · comments

Is it possible to use targeted catch blocks as it is with request-promise?

var errors = require('request-promise/errors');

rp('http://google.com')
.catch(errors.StatusCodeError, function (reason) {
// The server responded with a status codes other than 2xx.
// Check reason.statusCode
})
.catch(errors.RequestError, function (reason) {
// The request failed due to technical reasons.
// reason.cause is the Error object Request would pass into a callback.
});

When I use

var errors = require('request-promise-native/errors');

I am getting the following error

TypeError: Cannot set property 'name' of undefined
at StatusCodeError (/Users/mirene/Projects/Google/test/request-test/node_modules/request-promise-core/lib/errors.js:24:15)
at
at process._tickCallback (internal/process/next_tick.js:188:7)

Any ideas?

Those targeted catch blocks are a special feature of bluebird promises. If you want to use them please use request-promise which uses bluebird. However, native promises don’t support targeted catch blocks and thus you can’t use them when using request-promise-native.

This is hard to find samples catching StatusCodeError when using request-promise-native. I am not talking about targeted catch blocks, but only a way to determine an error caught by catch block is a StatusCodeError, using instanceof seems to not work in this case. By googling I always hit request-promise samples. Please document this common use case.