diff --git a/source/errors/HTTPError.ts b/source/errors/HTTPError.ts index 135500f0..f9b8c07c 100644 --- a/source/errors/HTTPError.ts +++ b/source/errors/HTTPError.ts @@ -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; diff --git a/test/browser.ts b/test/browser.ts index dd5ee5bb..54501cbb 100644 --- a/test/browser.ts +++ b/test/browser.ts @@ -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); diff --git a/test/hooks.ts b/test/hooks.ts index 73097190..783f8832 100644 --- a/test/hooks.ts +++ b/test/hooks.ts @@ -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); diff --git a/test/http-error.ts b/test/http-error.ts index a314076c..a1cf3025 100644 --- a/test/http-error.ts +++ b/test/http-error.ts @@ -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 => { @@ -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 => { @@ -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'); });