-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.ts
45 lines (38 loc) · 1.04 KB
/
index.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
export default class AssertionError<T> extends Error {
name = 'AssertionError'
showDiff: boolean
[key: string]: any
constructor(message: string, props?: T, ssf?: Function) {
super(message)
// default values
this.message = message || 'Unspecified AssertionError';
this.showDiff = false;
// copy from properties
for (var key in props) {
if (!(key in this)) {
// @ts-ignore
this[key] = props[key];
}
}
// capture stack trace
if ((Error as any).captureStackTrace) {
(Error as any).captureStackTrace(this, ssf || AssertionError);
} else {
try {
throw new Error();
} catch(e: any) {
this.stack = e.stack;
}
}
}
// Allow errors to be converted to JSON for static transfer.
toJSON(stack: boolean): Record<string, unknown> {
const {...props} = this
// include stack if exists and not turned off
if (false !== stack && this.stack) {
props.stack = this.stack;
}
props.message = this.message
return props;
};
}