Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve HTTPError messages #333

Merged
merged 2 commits into from
May 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
});