microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/common/error/internalError.ts
55lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. |
| 3 | |
| 4 | export enum InternalErrorLevel { |
| 5 | Error, |
| 6 | Warning, |
| 7 | } |
| 8 | |
| 9 | export class InternalError extends Error { |
| 10 | public errorCode: number; |
| 11 | public errorLevel: InternalErrorLevel; |
| 12 | |
| 13 | public get isInternalError(): boolean { |
| 14 | return true; |
| 15 | } |
| 16 | |
| 17 | constructor( |
| 18 | errorCode: number, |
| 19 | message: string, |
| 20 | errorLevel: InternalErrorLevel = InternalErrorLevel.Error, |
| 21 | ) { |
| 22 | super(message); |
| 23 | this.errorCode = errorCode; |
| 24 | this.errorLevel = errorLevel; |
| 25 | this.message = errorCode > 0 ? message + ` (error code ${this.errorCode})` : message; |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | export class NestedError extends InternalError { |
| 30 | public innerError: Error | any; // Normally this should be an error, but we support any value |
| 31 | private _extras: any; |
| 32 | |
| 33 | constructor( |
| 34 | errorCode: number, |
| 35 | message: string, |
| 36 | innerError: any = null, |
| 37 | extras?: any, |
| 38 | errorLevel: InternalErrorLevel = InternalErrorLevel.Error, |
| 39 | ) { |
| 40 | super(errorCode, message, errorLevel); |
| 41 | this.innerError = innerError; |
| 42 | this.name = innerError ? innerError.name : null; |
| 43 | const innerMessage = innerError ? innerError.message : null; |
| 44 | this.message = innerMessage ? `${message}: ${innerMessage}` : message; |
| 45 | this._extras = extras; |
| 46 | } |
| 47 | |
| 48 | public get extras(): any { |
| 49 | return this._extras; |
| 50 | } |
| 51 | |
| 52 | public static getWrappedError(error: InternalError, innerError: any): NestedError { |
| 53 | return new NestedError(innerError.errorCode || error.errorCode, error.message, innerError); |
| 54 | } |
| 55 | } |
| 56 | |