Skip to content

Commit

Permalink
Add throwHttpErrors option (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
killynathan authored and szmarczak committed Sep 8, 2018
1 parent 02c3a7e commit 27a826c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ const timeout = (promise, ms) => Promise.race([
]);

class Ky {
constructor(input, {timeout = 10000, json, ...otherOptions}) {
constructor(input, {timeout = 10000, throwHttpErrors = true, json, ...otherOptions}) {
this._input = input;
this._retryCount = 0;

Expand All @@ -100,6 +100,7 @@ class Ky {
};

this._timeout = timeout;
this._throwHttpErrors = throwHttpErrors;

const headers = new window.Headers(this._options.headers || {});

Expand Down Expand Up @@ -149,7 +150,9 @@ class Ky {
return retry();
}

throw error;
if (this._throwHttpErrors) {
throw error;
}
}
};

Expand Down
9 changes: 9 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,15 @@ Default: `10000`

Timeout in milliseconds for getting a response.

### throwHttpErrors

Type: `boolean`<br>
Default: `true`

Throw a `HTTPError` for error responses (non-2xx status codes).

Setting this to `false` may be useful if you are checking for resource availability and are expecting error responses.

### ky.extend(defaultOptions)

Create a new `ky` instance with some defaults overridden with your own.
Expand Down
14 changes: 14 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,20 @@ test('timeout option', async t => {
await server.close();
});

test('throwHttpErrors option', async t => {
const server = await createTestServer();
server.get('/', (request, response) => {
response.sendStatus(500);
});

await t.notThrowsAsync(
ky.get(server.url, {throwHttpErrors: false}).text(),
/Internal Server Error/
);

await server.close();
});

test('ky.extend()', async t => {
const server = await createTestServer();
server.get('/', (request, response) => {
Expand Down

0 comments on commit 27a826c

Please sign in to comment.