-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathExtendedError.ts
66 lines (62 loc) · 1.84 KB
/
ExtendedError.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
import * as merge from 'deepmerge';
export class ExtendedError extends Error {
options: ExtendedErrorOptions;
constructor(message: string, options?: ExtendedErrorOptions) {
super(message);
if (options) {
this.options = options;
}
this.name = this.constructor.name;
this.message = message;
if (typeof Error.captureStackTrace === 'function') {
Error.captureStackTrace(this, this.constructor);
} else {
this.stack = (new Error(message)).stack;
}
}
}
export class Rethrow extends ExtendedError {
newStack: string | undefined;
original: Error | ExtendedError | Rethrow;
constructor(message: string, error: Error | ExtendedError | Rethrow, options?: ExtendedErrorOptions) {
if (!error || !message) {
throw new Error('Rethrow requires a message and error');
}
const errorOptions = (<ExtendedError>error).options;
let originalOptions: string = '';
if (errorOptions) {
originalOptions = JSON.stringify(errorOptions);
if (options) {
super(message, merge(errorOptions, options));
} else {
super(message, errorOptions);
}
} else {
if (options) {
super(message, options);
} else {
super(message);
}
}
this.original = error;
if (originalOptions) {
(<ExtendedError>this.original).options = JSON.parse(originalOptions);
}
this.newStack = this.stack;
const messageLines: number = (this.message.match(/\n/g) || []).length + 1;
if (this.stack) {
this.stack = `${this.stack.split('\n').slice(0, messageLines + 1).join('\n')}\n${error.stack}`;
} else {
this.stack = error.stack;
}
}
}
export interface ExtendedErrorOptions {
http?: ExtendedErrorOptionsHttp;
}
interface ExtendedErrorOptionsHttp {
statusCode?: number;
message?: string;
decorate?: any;
override?: any;
}