|
2 | 2 | // See LICENSE in the project root for license information. |
3 | 3 |
|
4 | 4 | /** |
5 | | - * An `Error` subclass that should be thrown to report an unexpected state, which may indicate a software bug. |
6 | | - * The application may handle this error by instructing the user to report the problem to the application maintainers. |
| 5 | + * An `Error` subclass that should be thrown to report an unexpected state that may indicate a software defect. |
| 6 | + * An application may handle this error by instructing the end user to report an issue to the application maintainers. |
| 7 | + * |
| 8 | + * @remarks |
| 9 | + * Do not use this class unless you intend to solicit bug reports from end users. |
7 | 10 | * |
8 | 11 | * @public |
9 | 12 | */ |
10 | 13 | export class InternalError extends Error { |
| 14 | + /** |
| 15 | + * The underlying error message, without the additional boilerplate for an `InternalError`. |
| 16 | + */ |
| 17 | + public readonly unformattedMessage: string; |
| 18 | + |
| 19 | + private static _formatMessage(unformattedMessage: string): string { |
| 20 | + return `Internal Error: ${unformattedMessage}\n\nYou have encountered a software defect. Please consider` |
| 21 | + + `reporting the issue to the maintainers of this application.`; |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * Constructs a new instance of the {@link InternalError} class. |
| 26 | + * |
| 27 | + * @param message - A message describing the error. This will be assigned to |
| 28 | + * {@link InternalError.unformattedMessage}. The `Error.message` field will have additional boilerplate |
| 29 | + * explaining that the user has encountered a software defect. |
| 30 | + */ |
11 | 31 | constructor(message: string) { |
12 | | - super('Internal Error: ' + message); |
| 32 | + super(InternalError._formatMessage(message)); |
13 | 33 |
|
14 | 34 | // Manually set the prototype, as we can no longer extend built-in classes like Error, Array, Map, etc. |
15 | 35 | // tslint:disable-next-line:max-line-length |
16 | 36 | // https://github.com/Microsoft/TypeScript-wiki/blob/master/Breaking-Changes.md#extending-built-ins-like-error-array-and-map-may-no-longer-work |
17 | 37 | // |
18 | 38 | // Note: the prototype must also be set on any classes which extend this one |
19 | 39 | (this as any).__proto__ = InternalError.prototype; // tslint:disable-line:no-any |
| 40 | + |
| 41 | + this.unformattedMessage = message; |
| 42 | + } |
| 43 | + |
| 44 | + /** @override */ |
| 45 | + public toString(): string { |
| 46 | + return this.message; // Avoid adding the "Error:" prefix |
20 | 47 | } |
21 | 48 | } |
0 commit comments