Skip to content

Commit 5a88943

Browse files
brandon93ssindresorhus
authored andcommitted
Add throwHttpErrors option (#451)
1 parent 3f427bb commit 5a88943

4 files changed

Lines changed: 21 additions & 3 deletions

File tree

index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ function asPromise(opts) {
337337
}
338338
}
339339

340-
if (statusCode !== 304 && (statusCode < 200 || statusCode > limitStatusCode)) {
340+
if (opts.throwHttpErrors && statusCode !== 304 && (statusCode < 200 || statusCode > limitStatusCode)) {
341341
throw new got.HTTPError(statusCode, res.statusMessage, res.headers, opts);
342342
}
343343

@@ -425,7 +425,7 @@ function asStream(opts) {
425425

426426
res.pipe(output);
427427

428-
if (statusCode !== 304 && (statusCode < 200 || statusCode > 299)) {
428+
if (opts.throwHttpErrors && statusCode !== 304 && (statusCode < 200 || statusCode > 299)) {
429429
proxy.emit('error', new got.HTTPError(statusCode, res.statusMessage, res.headers, opts), null, res);
430430
return;
431431
}
@@ -467,7 +467,8 @@ function normalizeArguments(url, opts) {
467467
retries: 2,
468468
cache: false,
469469
decompress: true,
470-
useElectronNet: false
470+
useElectronNet: false,
471+
throwHttpErrors: true
471472
},
472473
url,
473474
{

readme.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,14 @@ Default: `false`
206206

207207
When used in Electron, Got will use [`electron.net`](https://electronjs.org/docs/api/net/) instead of the Node.js `http` module. According to the Electron docs, it should be fully compatible, but it's not entirely. See [#315](https://github.com/sindresorhus/got/issues/315).
208208

209+
###### throwHttpErrors
210+
211+
Type: `boolean`<br>
212+
Default: `true`
213+
214+
Determines if a `got.HTTPError` is thrown for error responses (non-2xx status codes).
215+
216+
If this is disabled, requests that encounter an error status code will be resolved with the `response` instead of throwing. This may be useful if you are checking for resource availability and are expecting error responses.
209217

210218
#### Streams
211219

test/http.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ test('status code 304 doesn\'t throw', async t => {
6060
t.is(response.body, '');
6161
});
6262

63+
test('doesn\'t throw on throwHttpErrors === false', async t => {
64+
t.is((await got(`${s.url}/404`, {throwHttpErrors: false})).body, 'not');
65+
});
66+
6367
test('invalid protocol throws', async t => {
6468
const err = await t.throws(got('c:/nope.com', {json: true}));
6569
t.is(err.constructor, got.UnsupportedProtocolError);

test/stream.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ test('have error event #2', async t => {
8484
await t.throws(pEvent(stream, 'response'), /getaddrinfo ENOTFOUND/);
8585
});
8686

87+
test('have response event on throwHttpErrors === false', async t => {
88+
const response = await pEvent(got.stream(`${s.url}/error`, {throwHttpErrors: false}), 'response');
89+
t.is(response.statusCode, 404);
90+
});
91+
8792
test('accepts option.body as Stream', async t => {
8893
const stream = got.stream(`${s.url}/post`, {body: intoStream(['wow'])});
8994
const data = await pEvent(stream, 'data');

0 commit comments

Comments
 (0)