-
-
Notifications
You must be signed in to change notification settings - Fork 334
/
http-error.ts
47 lines (38 loc) 路 1.57 KB
/
http-error.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import test from 'ava';
import {HTTPError} from '../source/index.js';
import {type Mutable} from '../source/utils/types.js';
function createFakeResponse({status, statusText}: {status?: number; statusText?: string}): Response {
// Start with a realistic fetch Response.
const response: Partial<Mutable<Response>> = {...new Response()};
response.status = status;
response.statusText = statusText;
return response as Response;
}
test('HTTPError handles undefined response.statusText', t => {
const status = 500;
// @ts-expect-error missing Request
const error = new HTTPError(
// This simulates the case where a browser Response object does
// not define statusText, such as IE, Safari, etc.
// See https://developer.mozilla.org/en-US/docs/Web/API/Response/statusText#Browser_compatibility
createFakeResponse({statusText: undefined, status}),
);
t.is(error.message, 'Request failed with status code 500');
});
test('HTTPError handles undefined response.status', t => {
// @ts-expect-error missing Request
const error = new HTTPError(
// This simulates a catastrophic case where some unexpected
// response object was sent to HTTPError.
createFakeResponse({statusText: undefined, status: undefined}),
);
t.is(error.message, 'Request failed with an unknown error');
});
test('HTTPError handles a response.status of 0', t => {
// @ts-expect-error missing Request
const error = new HTTPError(
// Apparently, it's possible to get a response status of 0.
createFakeResponse({statusText: undefined, status: 0}),
);
t.is(error.message, 'Request failed with status code 0');
});