microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/common/entryPointHandler.ts
64lines · 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 | |
| 5 | import {ErrorHelper} from "../common/error/errorHelper"; |
| 6 | import {InternalError} from "../common/error/internalError"; |
| 7 | import {InternalErrorCode} from "../common/error/internalErrorCode"; |
| 8 | import {TelemetryHelper} from "../common/telemetryHelper"; |
| 9 | import {Telemetry} from "../common/telemetry"; |
| 10 | import {Log} from "../common/log/log"; |
| 11 | import {ILogger} from "../common/log/loggers"; |
| 12 | |
| 13 | /* This class should we used for each entry point of the code, so we handle telemetry and error reporting properly */ |
| 14 | export class EntryPointHandler { |
| 15 | private isDebugeeProcess: boolean; |
| 16 | |
| 17 | constructor(isDebugeeProcess: boolean = false, logger?: ILogger) { |
| 18 | if (logger) { |
| 19 | Log.SetGlobalLogger(logger); |
| 20 | } |
| 21 | |
| 22 | this.isDebugeeProcess = isDebugeeProcess; |
| 23 | } |
| 24 | |
| 25 | |
| 26 | /* This method should wrap any async entry points to the code, so we handle telemetry and error reporting properly */ |
| 27 | public runFunction(taskName: string, error: InternalError, codeToRun: () => Q.Promise<void> | void, errorsAreFatal: boolean = false): void { |
| 28 | return this.handleErrors(error, TelemetryHelper.generate(taskName, codeToRun), /*errorsAreFatal*/ errorsAreFatal); |
| 29 | } |
| 30 | |
| 31 | /* This method should wrap the entry point of the whole app, so we handle telemetry and error reporting properly */ |
| 32 | public runApp(appName: string, getAppVersion: () => string, error: InternalError, codeToRun: () => Q.Promise<void>): void { |
| 33 | let telemetryError = ErrorHelper.getInternalError(InternalErrorCode.TelemetryInitializationFailed, error.message); |
| 34 | try { // try-catch for sync errors in init telemetry |
| 35 | return this.handleErrors(telemetryError, // handleErrors for async errors in init telemetry |
| 36 | Telemetry.init("react-native", getAppVersion(), true).then(() => |
| 37 | // After telemetry is initialized, we run the code. Errors in this main path are fatal so we rethrow them |
| 38 | this.runFunction(appName, error, codeToRun, /*errorsAreFatal*/ true)), /*errorsAreFatal*/ true); |
| 39 | } catch (error) { |
| 40 | Log.logError(ErrorHelper.wrapError(telemetryError, error), /*logStack*/ false); |
| 41 | throw error; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | private handleErrors(error: InternalError, resultOfCode: Q.Promise<void>, errorsAreFatal: boolean): void { |
| 46 | resultOfCode.done(() => { }, reason => { |
| 47 | const shouldLogStack = !errorsAreFatal || this.isDebugeeProcess; |
| 48 | Log.logError(ErrorHelper.wrapError(error, reason), /*logStack*/ shouldLogStack); |
| 49 | if (errorsAreFatal) { |
| 50 | /* The process is likely going to exit if errors are fatal, so we first |
| 51 | send the telemetry, and then we exit or rethrow the exception */ |
| 52 | Telemetry.sendPendingData().finally(() => { |
| 53 | if (this.isDebugeeProcess) { |
| 54 | /* HACK: For the debugee process we don't want to throw an exception because the debugger |
| 55 | will appear to the user if he turned on the VS Code uncaught exceptions feature. */ |
| 56 | process.exit(1); |
| 57 | } else { |
| 58 | throw reason; |
| 59 | } |
| 60 | }).done(); |
| 61 | } |
| 62 | }); |
| 63 | } |
| 64 | } |