microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/common/error/errorHelper.ts
64lines · modeblame
17161993Meena Kunnathur Balakrishnan10 years ago | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. | |
| 3 | | |
34472878RedMickey5 years ago | 4 | import { InternalError, NestedError, InternalErrorLevel } from "./internalError"; |
| 5 | import { InternalErrorCode } from "./internalErrorCode"; | |
| 6 | import { ERROR_STRINGS } from "./errorStrings"; | |
17161993Meena Kunnathur Balakrishnan10 years ago | 7 | |
| 8 | export class ErrorHelper { | |
3c172a05Artem Egorov8 years ago | 9 | public static ERROR_STRINGS = ERROR_STRINGS; |
34472878RedMickey5 years ago | 10 | public static getInternalError( |
| 11 | errorCode: InternalErrorCode, | |
| 12 | ...optionalArgs: any[] | |
| 13 | ): InternalError { | |
09f6024fHeniker4 years ago | 14 | const message = ErrorHelper.getErrorMessage(errorCode, ...optionalArgs); |
34472878RedMickey5 years ago | 15 | return new InternalError(<number>errorCode, message); |
17161993Meena Kunnathur Balakrishnan10 years ago | 16 | } |
| 17 | | |
34472878RedMickey5 years ago | 18 | public static getNestedError( |
| 19 | innerError: Error, | |
| 20 | errorCode: InternalErrorCode, | |
| 21 | ...optionalArgs: any[] | |
| 22 | ): NestedError { | |
09f6024fHeniker4 years ago | 23 | const message = ErrorHelper.getErrorMessage(errorCode, ...optionalArgs); |
34472878RedMickey5 years ago | 24 | return new NestedError(<number>errorCode, message, innerError); |
17161993Meena Kunnathur Balakrishnan10 years ago | 25 | } |
| 26 | | |
190e393cMeena Kunnathur Balakrishnan10 years ago | 27 | public static wrapError(error: InternalError, innerError: Error): NestedError { |
| 28 | return NestedError.getWrappedError(error, innerError); | |
| 29 | } | |
e416b901Yuri Skorokhodov7 years ago | 30 | |
34472878RedMickey5 years ago | 31 | public static getWarning(message: string): InternalError { |
190e393cMeena Kunnathur Balakrishnan10 years ago | 32 | return new InternalError(-1, message, InternalErrorLevel.Warning); |
| 33 | } | |
| 34 | | |
34472878RedMickey5 years ago | 35 | public static getNestedWarning(innerError: Error, message: string): NestedError { |
| 36 | return new NestedError( | |
| 37 | -1, | |
| 38 | message, | |
| 39 | innerError, | |
| 40 | null /* extras */, | |
| 41 | InternalErrorLevel.Warning, | |
| 42 | ); | |
a4a7e387Meena Kunnathur Balakrishnan10 years ago | 43 | } |
| 44 | | |
17161993Meena Kunnathur Balakrishnan10 years ago | 45 | private static getErrorMessage(errorCode: InternalErrorCode, ...optionalArgs: any[]): string { |
34472878RedMickey5 years ago | 46 | return ErrorHelper.formatErrorMessage( |
5b09cf97Zhen Zhen Yuan (BEYONDSOFT CONSULTING INC)7 months ago | 47 | ErrorHelper.ERROR_STRINGS[errorCode as keyof typeof ErrorHelper.ERROR_STRINGS], |
34472878RedMickey5 years ago | 48 | ...optionalArgs, |
| 49 | ); | |
17161993Meena Kunnathur Balakrishnan10 years ago | 50 | } |
| 51 | | |
| 52 | private static formatErrorMessage(errorMessage: string, ...optionalArgs: any[]): string { | |
4f8669f9JiglioNero6 years ago | 53 | if (!errorMessage) { |
| 54 | return errorMessage; | |
| 55 | } | |
a4a7e387Meena Kunnathur Balakrishnan10 years ago | 56 | |
34472878RedMickey5 years ago | 57 | let result: string = <string>errorMessage; |
09f6024fHeniker4 years ago | 58 | for (const [i, optionalArg] of optionalArgs.entries()) { |
| 59 | result = result.replace(new RegExp(`\\{${i}\\}`, "g"), optionalArg); | |
4f8669f9JiglioNero6 years ago | 60 | } |
17161993Meena Kunnathur Balakrishnan10 years ago | 61 | |
4f8669f9JiglioNero6 years ago | 62 | return result; |
17161993Meena Kunnathur Balakrishnan10 years ago | 63 | } |
| 64 | } |