-
Notifications
You must be signed in to change notification settings - Fork 14
/
errors.ts
111 lines (100 loc) · 2.25 KB
/
errors.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/**
* A custom error class for input errors.
*
* @example
* const fn = () => {
* throw new InputError('Invalid input', 'user.name')
* }
*/
class InputError extends Error {
/**
* Path of input attribute that originated the error.
*/
path: string[]
constructor(message: string, path: string[] = []) {
super(message)
this.name = 'InputError'
this.path = path
}
}
/**
* @deprecated Use `ContextError` instead
* A custom error class for context errors.
*
* @example
* const fn = () => {
* throw new EnvironmentError('Invalid environment', 'user.name')
* }
*/
class EnvironmentError extends Error {
/**
* Path of context attribute that originated the error.
*/
path: string[]
constructor(message: string, path: string[] = []) {
super(message)
this.name = 'EnvironmentError'
this.path = path
}
}
/**
* A custom error class for context errors.
*
* @example
* const fn = () => {
* throw new ContextError('Invalid context', 'user.name')
* }
*/
class ContextError extends Error {
/**
* Path of context attribute that originated the error.
*/
path: string[]
constructor(message: string, path: string[] = []) {
super(message)
this.name = 'ContextError'
this.path = path
}
}
/**
* A list of errors
*
* Useful to propagate error from mutiple composables in parallel execution
*/
class ErrorList extends Error {
/**
* The list of errors
*/
list: Error[]
constructor(errors: Error[]) {
super('ErrorList')
this.name = 'ErrorList'
this.list = errors
}
}
/**
* A function to check if an `Error` or a `SerializableError` is an InputError
*/
function isInputError(e: { name: string; message: string }): boolean {
return e.name === 'InputError'
}
/**
* @deprecated Use `isContextError` instead
* A function to check if an `Error` or a `SerializableError` is a ContextError
*/
const isEnvironmentError = isContextError
/**
* A function to check if an `Error` or a `SerializableError` is a ContextError
*/
function isContextError(e: { name: string; message: string }): boolean {
return e.name === 'EnvironmentError' || e.name === 'ContextError'
}
export {
ContextError,
EnvironmentError,
ErrorList,
InputError,
isContextError,
isEnvironmentError,
isInputError,
}