-
Notifications
You must be signed in to change notification settings - Fork 622
/
Copy pathParseError.ts
62 lines (52 loc) · 1.91 KB
/
ParseError.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
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import type { TextRange, ITextLocation } from './TextRange';
/**
* An Error subclass used to report errors that occur while parsing an input.
*/
export class ParseError extends Error {
/**
* The text range where the error occurred.
*/
public readonly range: TextRange;
/**
* The message string passed to the constructor, before the line/column
* numbering information was added.
*/
public readonly unformattedMessage: string;
/**
* The underlying error, if this error is resulted from an earlier error.
*/
public readonly innerError: Error | undefined;
public constructor(message: string, range: TextRange, innerError?: Error) {
super(ParseError._formatMessage(message, range));
// Boilerplate for extending a system class
//
// https://github.com/microsoft/TypeScript-wiki/blob/main/Breaking-Changes.md#extending-built-ins-like-error-array-and-map-may-no-longer-work
//
// IMPORTANT: The prototype must also be set on any classes which extend this one
(this as any).__proto__ = ParseError.prototype; // eslint-disable-line @typescript-eslint/no-explicit-any
this.unformattedMessage = message;
this.range = range;
this.innerError = innerError;
}
/**
* Generates a line/column prefix. Example with line=2 and column=5
* and message="An error occurred":
* ```
* "(2,5): An error occurred"
* ```
*/
private static _formatMessage(message: string, range: TextRange): string {
if (!message) {
message = 'An unknown error occurred';
}
if (range.pos !== 0 || range.end !== 0) {
const location: ITextLocation = range.getLocation(range.pos);
if (location.line) {
return `(${location.line},${location.column}): ` + message;
}
}
return message;
}
}