Skip to content

Commit 50fed0f

Browse files
authored
Reject promises with request-promise StatusCodeError (#2)
1 parent 282c849 commit 50fed0f

File tree

4 files changed

+36
-20
lines changed

4 files changed

+36
-20
lines changed

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ var request = require('request');
1313
var RetryStrategies = require('./strategies');
1414
var isErrorResponse = require('./helpers/isErrorResponse')
1515
var _ = require('lodash');
16+
var { StatusCodeError } = require('request-promise-core/errors');
1617

1718
var DEFAULTS = {
1819
maxAttempts: 5, // try 5 times
@@ -111,7 +112,7 @@ function Request(url, options, f, retryConfig) {
111112
}
112113

113114
if (isErrorResponse(response)) {
114-
return this._reject(this.fullResponse ? response : body);
115+
return this._reject(new StatusCodeError(response.statusCode, body, this.options, response));
115116
}
116117

117118
// resolve with the full response or just the body

package-lock.json

Lines changed: 20 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@hmcts/requestretry",
33
"description": "request-retry wrap nodejs request to retry http(s) requests in case of error",
4-
"version": "1.0.1",
4+
"version": "1.1.0",
55
"author": {
66
"name": "Francois-Guillaume Ribreau",
77
"email": "npm@fgribreau.com",
@@ -46,6 +46,7 @@
4646
"extend": "^3.0.0",
4747
"lodash": "^4.15.0",
4848
"request": "^2.74.0",
49+
"request-promise-core": "^1.1.1",
4950
"when": "^3.7.7"
5051
},
5152
"devDependencies": {

test/promises.test.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ describe('Promises support', function () {
9696
retryStrategy: request.RetryStrategies.HTTPOrNetworkError
9797
})
9898
.catch(function (err) {
99-
t.strictEqual(err.body, 'Client Error');
99+
t.strictEqual(err.statusCode, 400);
100+
t.strictEqual(err.error, 'Client Error');
100101
done();
101102
});
102103
});
@@ -112,7 +113,8 @@ describe('Promises support', function () {
112113
retryStrategy: request.RetryStrategies.HTTPOrNetworkError
113114
})
114115
.catch(function (err) {
115-
t.strictEqual(err.body, 'Server Error');
116+
t.strictEqual(err.statusCode, 500);
117+
t.strictEqual(err.error, 'Server Error');
116118
done();
117119
});
118120
});
@@ -162,7 +164,8 @@ describe('Promises support', function () {
162164
it('should work on request fail', function (done) {
163165
makeRequest(customPromiseFactory, done, true)
164166
.catch(function (err) {
165-
t.strictEqual(err, 'Internal Server Error');
167+
t.strictEqual(err.statusCode, 500);
168+
t.strictEqual(err.error, 'Internal Server Error');
166169
done();
167170
});
168171
});
@@ -180,7 +183,8 @@ describe('Promises support', function () {
180183
it('should work on request fail', function (done) {
181184
makeRequest(customPromiseFactory, done, true)
182185
.catch(function (err) {
183-
t.strictEqual(err, 'Internal Server Error');
186+
t.strictEqual(err.statusCode, 500);
187+
t.strictEqual(err.error, 'Internal Server Error');
184188
done();
185189
});
186190
});
@@ -203,7 +207,8 @@ describe('Promises support', function () {
203207
it('should work on request fail', function (done) {
204208
makeRequest(customPromiseFactory, done, true)
205209
.fail(function (err) {
206-
t.strictEqual(err, 'Internal Server Error');
210+
t.strictEqual(err.statusCode, 500);
211+
t.strictEqual(err.error, 'Internal Server Error');
207212
done();
208213
});
209214
});
@@ -223,7 +228,8 @@ describe('Promises support', function () {
223228
it('should work on request fail', function (done) {
224229
makeRequest(customPromiseFactory, done, true)
225230
.catch(function (err) {
226-
t.strictEqual(err, 'Internal Server Error');
231+
t.strictEqual(err.statusCode, 500);
232+
t.strictEqual(err.error, 'Internal Server Error');
227233
done();
228234
});
229235
});

0 commit comments

Comments
 (0)