-
Notifications
You must be signed in to change notification settings - Fork 774
Expand file tree
/
Copy patherror-handler.ts
More file actions
71 lines (61 loc) · 1.86 KB
/
error-handler.ts
File metadata and controls
71 lines (61 loc) · 1.86 KB
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
/* tslint:disable:max-classes-per-file */
export declare class Error {
public name: string;
public message: string;
public index: number;
public lineNumber: number;
public column: number;
public description: string;
constructor(message: string);
}
export class ErrorHandler {
readonly errors: Error[];
tolerant: boolean;
constructor() {
this.errors = [];
this.tolerant = false;
}
recordError(error: Error): void {
this.errors.push(error);
}
tolerate(error): void {
if (this.tolerant) {
this.recordError(error);
} else {
throw error;
}
}
constructError(msg: string, column: number): Error {
let error = new Error(msg);
try {
throw error;
} catch (base) {
/* istanbul ignore else */
if (Object.create && Object.defineProperty) {
error = Object.create(base);
Object.defineProperty(error, 'column', { value: column });
}
}
/* istanbul ignore next */
return error;
}
createError(index: number, line: number, col: number, description: string): Error {
const msg = 'Line ' + line + ': ' + description;
const error = this.constructError(msg, col);
error.index = index;
error.lineNumber = line;
error.description = description;
return error;
}
throwError(index: number, line: number, col: number, description: string): never {
throw this.createError(index, line, col, description);
}
tolerateError(index: number, line: number, col: number, description: string) {
const error = this.createError(index, line, col, description);
if (this.tolerant) {
this.recordError(error);
} else {
throw error;
}
}
}