Skip to content

Commit ea4e375

Browse files
lechinoixOjisama
authored andcommitted
feat(request-manager): fail on status_code >= 400
1 parent c58bd58 commit ea4e375

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

__tests__/util/request-manager.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,3 +225,27 @@ test('RequestManager.saveHar no captureHar error message', async () => {
225225
expect(err.message).toBe('RequestManager was not setup to capture HAR files');
226226
}
227227
});
228+
229+
test('RequestManager.execute Request 4xx or 5xx error', async () => {
230+
jest.resetModules();
231+
const config = await Config.create({}, new Reporter());
232+
jest.mock('request', factory => options => {
233+
const errorStatusCode = Math.floor(Math.random() * (600 - 400) + 400); // random error code between 400 and 599
234+
options.callback('', {statusCode: errorStatusCode}, '');
235+
return {
236+
on: () => {},
237+
};
238+
});
239+
await config.requestManager.execute({
240+
params: {
241+
url: `https://localhost:port/?nocache`,
242+
headers: {Connection: 'close'},
243+
},
244+
resolve: body => {},
245+
reject: err => {
246+
expect(
247+
err.message.startsWith('https://localhost:port/?nocache: Request "https://localhost:port/?nocache" returned a'),
248+
).toBe(true);
249+
},
250+
});
251+
});

src/util/request-manager.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -388,21 +388,20 @@ export default class RequestManager {
388388
successHosts[parts.hostname] = true;
389389

390390
this.reporter.verbose(this.reporter.lang('verboseRequestFinish', params.url, res.statusCode));
391-
392391
if (body && typeof body.error === 'string') {
393392
reject(new Error(body.error));
394393
return;
395394
}
396395

397-
if (res.statusCode === 403) {
398-
const errMsg = (body && body.message) || reporter.lang('requestError', params.url, res.statusCode);
399-
reject(new Error(errMsg));
396+
if ([400, 401, 404].concat(params.rejectStatusCode || []).indexOf(res.statusCode) !== -1) {
397+
body = false;
400398
} else {
401-
if ([400, 401, 404].concat(params.rejectStatusCode || []).indexOf(res.statusCode) !== -1) {
402-
body = false;
399+
if (res.statusCode >= 400) {
400+
const errMsg = (body && body.message) || reporter.lang('requestError', params.url, res.statusCode);
401+
reject(new Error(errMsg));
403402
}
404-
resolve(body);
405403
}
404+
resolve(body);
406405
};
407406
}
408407

0 commit comments

Comments
 (0)