-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.ts
55 lines (51 loc) · 1.82 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
/**
* Represents an Error with optional inner error.
*/
export interface INestableError {
readonly inner?: any;
getBaseError(): any;
}
class RawValueError extends Error implements INestableError {
public readonly name = "RawValueError";
constructor(public readonly rawValue: any, public readonly inner?: any) {
super();
Object.setPrototypeOf(this, RawValueError);
}
public readonly getBaseError = getBaseError;
}
function getBaseError(this: INestableError) {
let current = this;
while (current.inner != null) {
current = current.inner;
}
return current;
}
export function setInnerError(error: any, inner: any): Error & INestableError {
if (error instanceof Error) {
Object.defineProperty(error, "inner", { value: inner, writable: false });
Object.defineProperty(error, "getBaseError", { value: getBaseError, writable: false });
return error as (Error & INestableError);
} else {
return new RawValueError(error, inner);
}
}
export class AggregateError extends Error {
public readonly name = "RawValueError";
public readonly innerErrors: readonly any[];
constructor(message?: string, ...errors: any[]) {
super();
Object.setPrototypeOf(this, AggregateError);
this.innerErrors = errors.filter(e => e != null);
}
public forEach<TThis>(callback: (this: TThis, error: any) => void, thisArg: TThis): void;
public forEach(callback: (this: void, error: any) => void): void;
public forEach(callback: (this: unknown, error: any) => void, thisArg?: unknown): void {
callback.call(thisArg, this);
for (const e of this.innerErrors) {
callback.call(thisArg, e);
if (e instanceof AggregateError) {
e.forEach(callback, thisArg);
}
}
}
}