-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfaults.ts
155 lines (138 loc) Β· 4.04 KB
/
faults.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import { errorType } from './errors';
import { BaseException, Exception } from './exceptions';
import { ExitCode, ExitCoded } from './exit';
import { Formattable, Formatter, formatter } from './format';
import { Traceable, Traceback } from './traceback';
/**
* A base class for all faults.
*/
@errorType('BaseFault')
export class BaseFault extends Error implements Traceable, Formattable {
/**
* @param message The message for the fault.
* @param traceback The traceback for the fault, if applicable.
*/
constructor(
message: any,
public traceback: Traceback | null = null,
) {
super(formatter.format(message));
Object.setPrototypeOf(this, new.target.prototype);
}
format(formatter: Formatter): string {
return formatter.formatBaseFault(this);
}
}
/**
* A fault. Faults are a problem with the interpreter, not the source code,
* and are always fatal.
*/
@errorType('Fault')
export class Fault extends BaseFault {}
/**
* A fault for any JavaScript Error which can't be represented as an
* Exception. All flagrant faults are indicative of interpreter bugs.
*/
@errorType('RuntimeFault')
export class RuntimeFault extends Fault implements ExitCoded {
public exitCode = ExitCode.Software;
/**
* @param message The message for the fault.
* @param error The underlying JavaScript error. This may be an
* assert.AssertionError.
* @param traceback The traceback for the fault.
*/
constructor(
message: any,
public error: Error,
traceback: Traceback | null = null,
) {
super(message, traceback);
Object.setPrototypeOf(this, new.target.prototype);
}
static fromError(err: any, traceback: Traceback | null = null): RuntimeFault {
let message = 'Unspecified error';
if (typeof err.message === 'undefined') {
if (err.format) {
try {
message = formatter.format(err);
} catch (err) {
message = String(err);
}
} else {
message = String(err);
}
} else if (typeof err.message === 'string') {
message = err.message;
} else if (err.format) {
try {
message = formatter.format(err.message);
} catch (err) {
message = String(err.message);
}
} else {
message = String(err.message);
}
if (!(err instanceof Error)) {
err = new Error(message);
}
return new RuntimeFault(message, err, traceback);
}
static fromException(exc: BaseException): RuntimeFault {
return new RuntimeFault(
exc.message,
new Error(`Uncaught ${exc.constructor.name}: ${exc.message}`),
exc.traceback,
);
}
format(formatter: Formatter): string {
return formatter.formatRuntimeFault(this);
}
}
/**
* A fault raised when functionality is not implemented. Extends RuntimeFault.
*/
@errorType('NotImplementedFault')
export class NotImplementedFault extends RuntimeFault {
constructor(message: any, traceback: Traceback | null = null) {
super(message, new Error(), traceback);
Object.setPrototypeOf(this, new.target.prototype);
this.error = this;
}
}
/**
* A fault raised when the command was called with invalid arguments.
*/
@errorType('UsageFault')
export class UsageFault extends Fault implements ExitCoded {
public exitCode = ExitCode.Usage;
constructor(message: string) {
super(message, null);
Object.setPrototypeOf(this, new.target.prototype);
}
format(formatter: Formatter): string {
return formatter.formatUsageFault(this);
}
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
export function runtimeMethod<F extends Function>(
_target: any,
_propertyKey: string,
descriptor: PropertyDescriptor,
) {
const fn: F = descriptor.value;
const wrapped: F = function captured(...args: any[]): any {
try {
return fn.apply(this, args);
} catch (err) {
if (err instanceof Exception) {
throw err;
}
if (!(err instanceof BaseFault)) {
throw RuntimeFault.fromError(err, null);
}
throw err;
}
} as unknown as F;
descriptor.value = wrapped;
}