Skip to content

Commit

Permalink
Improve HTTPError messages (#333)
Browse files Browse the repository at this point in the history
  • Loading branch information
sholladay committed May 4, 2021
1 parent 453846e commit ca098f8
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 11 deletions.
12 changes: 6 additions & 6 deletions source/errors/HTTPError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ export class HTTPError extends Error {
public options: NormalizedOptions;

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

super(`Request failed with ${reason}`);

this.name = 'HTTPError';
this.response = response;
Expand Down
2 changes: 1 addition & 1 deletion test/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ test('retry with body', withPage, async (t: ExecutionContext, page: Page) => {
retry: 2
});
}, server.url),
{message: /HTTPError: Bad Gateway/}
{message: /HTTPError: Request failed with status code 502 Bad Gateway/}
);

t.is(requestCount, 2);
Expand Down
2 changes: 1 addition & 1 deletion test/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ test('beforeRetry hook with parseJson and error.response.json()', async t => {
beforeRetry: [
async ({error, retryCount}) => {
t.true(error instanceof ky.HTTPError);
t.is(error.message, 'Bad Gateway');
t.is(error.message, 'Request failed with status code 502 Bad Gateway');
t.true((error as HTTPError).response instanceof Response);
t.deepEqual(await (error as HTTPError).response.json(), {awesome: true});
t.is(retryCount, 1);
Expand Down
6 changes: 3 additions & 3 deletions test/http-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ test('HTTPError handles undefined response.statusText', t => {
createFakeResponse({statusText: undefined, status})
);

t.is(error.message, String(status));
t.is(error.message, 'Request failed with status code 500');
});

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

t.is(error.message, 'Unknown response error');
t.is(error.message, 'Request failed with an unknown error');
});

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

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

0 comments on commit ca098f8

Please sign in to comment.