Skip to content

Commit ca098f8

Browse files
authoredMay 4, 2021
Improve HTTPError messages (#333)
1 parent 453846e commit ca098f8

File tree

4 files changed

+11
-11
lines changed

4 files changed

+11
-11
lines changed
 

‎source/errors/HTTPError.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ export class HTTPError extends Error {
66
public options: NormalizedOptions;
77

88
constructor(response: Response, request: Request, options: NormalizedOptions) {
9-
// Set the message to the status text, such as Unauthorized,
10-
// with some fallbacks. This message should never be undefined.
11-
super(
12-
response.statusText ||
13-
String(response.status === 0 || response.status ? response.status : 'Unknown response error')
14-
);
9+
const code = (response.status || response.status === 0) ? response.status : '';
10+
const title = response.statusText || '';
11+
const status = `${code} ${title}`.trim();
12+
const reason = status ? `status code ${status}` : 'an unknown error';
13+
14+
super(`Request failed with ${reason}`);
1515

1616
this.name = 'HTTPError';
1717
this.response = response;

‎test/browser.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ test('retry with body', withPage, async (t: ExecutionContext, page: Page) => {
405405
retry: 2
406406
});
407407
}, server.url),
408-
{message: /HTTPError: Bad Gateway/}
408+
{message: /HTTPError: Request failed with status code 502 Bad Gateway/}
409409
);
410410

411411
t.is(requestCount, 2);

‎test/hooks.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ test('beforeRetry hook with parseJson and error.response.json()', async t => {
483483
beforeRetry: [
484484
async ({error, retryCount}) => {
485485
t.true(error instanceof ky.HTTPError);
486-
t.is(error.message, 'Bad Gateway');
486+
t.is(error.message, 'Request failed with status code 502 Bad Gateway');
487487
t.true((error as HTTPError).response instanceof Response);
488488
t.deepEqual(await (error as HTTPError).response.json(), {awesome: true});
489489
t.is(retryCount, 1);

‎test/http-error.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ test('HTTPError handles undefined response.statusText', t => {
2525
createFakeResponse({statusText: undefined, status})
2626
);
2727

28-
t.is(error.message, String(status));
28+
t.is(error.message, 'Request failed with status code 500');
2929
});
3030

3131
test('HTTPError handles undefined response.status', t => {
@@ -36,7 +36,7 @@ test('HTTPError handles undefined response.status', t => {
3636
createFakeResponse({statusText: undefined, status: undefined})
3737
);
3838

39-
t.is(error.message, 'Unknown response error');
39+
t.is(error.message, 'Request failed with an unknown error');
4040
});
4141

4242
test('HTTPError handles a response.status of 0', t => {
@@ -46,5 +46,5 @@ test('HTTPError handles a response.status of 0', t => {
4646
createFakeResponse({statusText: undefined, status: 0})
4747
);
4848

49-
t.is(error.message, '0');
49+
t.is(error.message, 'Request failed with status code 0');
5050
});

0 commit comments

Comments
 (0)
Please sign in to comment.