-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHTTPException.js
69 lines (55 loc) · 1.23 KB
/
HTTPException.js
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
* @copyright 2023 Chris Zuber <[email protected]>
*/
const symbols = { resp: Symbol('resp') };
export class HTTPException extends Error {
constructor(resp = Response.error(), { message, cause } = {}) {
super(
typeof message === 'string' ? message :`<${resp.url}> [${resp.status} ${resp.statusText}]`,
{ cause }
);
Object.defineProperty(this, symbols.resp, {
value: resp,
configurable: false,
writable: false,
enumerable: false,
});
}
async json() {
return await this[symbols.resp].json();
}
async text() {
return await this[symbols.resp].text();
}
get body() {
return this[symbols.resp].body;
}
get bodyUsed() {
return this[symbols.resp].bodyUsed;
}
get headers() {
return this[symbols.resp].headers;
}
get ok() {
return this[symbols.resp].ok;
}
get redirected() {
return this[symbols.resp].redirected;
}
get status() {
return this[symbols.resp].status;
}
get statusText() {
return this[symbols.resp].statusText;
}
get type() {
return this[symbols.resp].type;
}
get url() {
return this[symbols.resp].url;
}
toJSON() {
const { status, statusText, ok, url, message, type, redirected } = this;
return { status, ok, statusText, url, message, type, redirected };
}
}