-
Notifications
You must be signed in to change notification settings - Fork 486
Expand file tree
/
Copy patherror.ts
More file actions
50 lines (43 loc) · 877 Bytes
/
error.ts
File metadata and controls
50 lines (43 loc) · 877 Bytes
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
export type ErrorOr<T, E extends ErrorObject = ErrorObject> =
| Success<T>
| Failure<E>
export type Success<T> = {
success: true
value: T
}
export type Failure<E extends ErrorObject = ErrorObject> = {
success: false
error: E
}
export type ErrorObject = {
name: string
message: string
stack?: string
rawError?: string
}
export function success<T>(value: T): Success<T> {
return {
success: true,
value,
}
}
export function failure(error: any): Failure<ErrorObject> {
return {
success: false,
error: getErrorObject(error),
}
}
export function getErrorObject(error: any): ErrorObject {
if (error instanceof Error) {
return {
name: error.name,
message: error.message,
stack: error.stack,
rawError: JSON.stringify(error, null, 2),
}
}
return {
name: 'Error',
message: `${error}`,
}
}