-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patherror.ts
46 lines (40 loc) · 1.12 KB
/
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
export interface TypedError<Name extends string, Cause extends Error = Error>
extends Error {
/**
* The error name
*/
readonly name: Name;
/**
* The error cause
*/
readonly cause?: Cause;
}
export const makeError = <Name extends string, Cause extends Error = Error>(
name: Name,
message?: string,
cause?: Cause,
): TypedError<Name, Cause> => {
// from https://stackoverflow.com/a/43001581
type Writeable<T> = { -readonly [P in keyof T]: T[P] };
const error = new Error(message, { cause }) as Writeable<
TypedError<Name, Cause>
>;
error.name = name;
return error;
};
export interface HTTPError<Cause extends Error = Error>
extends TypedError<"HTTPError", Cause> {
readonly response: Response;
}
export const makeHTTPError = <Cause extends Error = Error>(
response: Response,
message?: string,
cause?: Cause,
): HTTPError<Cause> => {
// from https://stackoverflow.com/a/43001581
type Writeable<T> = { -readonly [P in keyof T]: T[P] };
const error = new Error(message, { cause }) as Writeable<HTTPError<Cause>>;
error.name = "HTTPError";
error.response = response;
return error;
};