microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/common/error/errorHelper.ts
63lines · 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 | import {InternalError, NestedError, InternalErrorLevel} from "./internalError"; |
| 5 | import {InternalErrorCode} from "./internalErrorCode"; |
| 6 | import {ERROR_STRINGS} from "./errorStrings"; |
| 7 | |
| 8 | export class ErrorHelper { |
| 9 | public static ERROR_STRINGS = ERROR_STRINGS; |
| 10 | public static getInternalError(errorCode: InternalErrorCode, ...optionalArgs: any[]): InternalError { |
| 11 | let message = ErrorHelper.getErrorMessage(errorCode, ...optionalArgs); |
| 12 | return new InternalError(<number> errorCode, message); |
| 13 | } |
| 14 | |
| 15 | public static getNestedError(innerError: Error, errorCode: InternalErrorCode, ...optionalArgs: any[]): NestedError { |
| 16 | let message = ErrorHelper.getErrorMessage(errorCode, ...optionalArgs); |
| 17 | return new NestedError(<number> errorCode, message, innerError); |
| 18 | } |
| 19 | |
| 20 | public static wrapError(error: InternalError, innerError: Error): NestedError { |
| 21 | return NestedError.getWrappedError(error, innerError); |
| 22 | } |
| 23 | |
| 24 | public static getWarning(message: string, ...optionalArgs: any[]): InternalError { |
| 25 | return new InternalError(-1, message, InternalErrorLevel.Warning); |
| 26 | } |
| 27 | |
| 28 | public static getNestedWarning(innerError: Error, message: string, ...optionalArgs: any[]): NestedError { |
| 29 | return new NestedError(-1, message, innerError, null /* extras */, InternalErrorLevel.Warning); |
| 30 | } |
| 31 | |
| 32 | private static getErrorMessage(errorCode: InternalErrorCode, ...optionalArgs: any[]): string { |
| 33 | return ErrorHelper.formatErrorMessage(ErrorHelper.ERROR_STRINGS[errorCode], ...optionalArgs); |
| 34 | } |
| 35 | |
| 36 | private static formatErrorMessage(errorMessage: string, ...optionalArgs: any[]): string { |
| 37 | if (!errorMessage) { |
| 38 | return errorMessage; |
| 39 | } |
| 40 | |
| 41 | let result: string = <string> errorMessage; |
| 42 | let args: string[] = ErrorHelper.getOptionalArgsArrayFromFunctionCall(arguments, 1); |
| 43 | if (args) { |
| 44 | for (let i: number = 0; i < args.length; i++) { |
| 45 | result = result.replace(new RegExp("\\{" + i + "\\}", "g"), args[i]); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | return result; |
| 50 | } |
| 51 | |
| 52 | private static getOptionalArgsArrayFromFunctionCall(functionArguments: IArguments, startIndex: number): any[] { |
| 53 | if (functionArguments.length <= startIndex) { |
| 54 | return []; |
| 55 | } |
| 56 | |
| 57 | if (Array.isArray(functionArguments[startIndex])) { |
| 58 | return functionArguments[startIndex]; |
| 59 | } |
| 60 | |
| 61 | return Array.prototype.slice.apply(functionArguments, [startIndex]); |
| 62 | } |
| 63 | } |
| 64 | |