-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathHttpError.ts
29 lines (26 loc) · 956 Bytes
/
HttpError.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
/**
* An {@link Error} that wraps a {@link Response}.
*
* Thrown when the HTTP response is not OK (HTTP code status outside range 200 – 299).
*/
// Should be named HTTPError or HttpError?
// - [XML*Http*Request](https://developer.mozilla.org/en-US/docs/Web/API)
// - Node.js uses [http](https://nodejs.org/en/docs/guides/anatomy-of-an-http-transaction/)
// - Deno uses [Http](https://github.com/denoland/deno/blob/v1.5.3/cli/rt/01_errors.js#L116)
export class HttpError extends Error {
/**
* Undefined when using {@link createHttpError()} or {@link createResponsePromise()}.
*/
request: Request;
response: Response;
constructor(request: Request, response: Response) {
const { status, statusText } = response;
super(
// statusText can be empty: https://stackoverflow.com/q/41632077
statusText || String(status)
);
this.name = 'HttpError';
this.request = request;
this.response = response;
}
}