microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/common/entryPointHandler.ts
64lines · modeblame
8ee905e8digeff10 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 | | |
190e393cMeena Kunnathur Balakrishnan10 years ago | 4 | |
| 5 | import {ErrorHelper} from "../common/error/errorHelper"; | |
| 6 | import {InternalError} from "../common/error/internalError"; | |
| 7 | import {InternalErrorCode} from "../common/error/internalErrorCode"; | |
8e2d694fdigeff10 years ago | 8 | import {TelemetryHelper} from "../common/telemetryHelper"; |
| 9 | import {Telemetry} from "../common/telemetry"; | |
190e393cMeena Kunnathur Balakrishnan10 years ago | 10 | import {Log} from "../common/log/log"; |
e8771a03Meena Kunnathur Balakrishnan10 years ago | 11 | import {ILogger} from "../common/log/loggers"; |
10873e11digeff10 years ago | 12 | |
| 13 | /* This class should we used for each entry point of the code, so we handle telemetry and error reporting properly */ | |
47958817digeff10 years ago | 14 | export class EntryPointHandler { |
e8771a03Meena Kunnathur Balakrishnan10 years ago | 15 | private isDebugeeProcess: boolean; |
10873e11digeff10 years ago | 16 | |
e8771a03Meena Kunnathur Balakrishnan10 years ago | 17 | constructor(isDebugeeProcess: boolean = false, logger?: ILogger) { |
| 18 | if (logger) { | |
| 19 | Log.SetGlobalLogger(logger); | |
898cb3c6Meena Kunnathur Balakrishnan10 years ago | 20 | } |
e8771a03Meena Kunnathur Balakrishnan10 years ago | 21 | |
| 22 | this.isDebugeeProcess = isDebugeeProcess; | |
10873e11digeff10 years ago | 23 | } |
| 24 | | |
ac1bd6f1digeff10 years ago | 25 | |
10873e11digeff10 years ago | 26 | /* This method should wrap any async entry points to the code, so we handle telemetry and error reporting properly */ |
190e393cMeena Kunnathur Balakrishnan10 years ago | 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); | |
10873e11digeff10 years ago | 29 | } |
| 30 | | |
| 31 | /* This method should wrap the entry point of the whole app, so we handle telemetry and error reporting properly */ | |
190e393cMeena Kunnathur Balakrishnan10 years ago | 32 | public runApp(appName: string, getAppVersion: () => string, error: InternalError, codeToRun: () => Q.Promise<void>): void { |
a289475bMeena Kunnathur Balakrishnan10 years ago | 33 | let telemetryError = ErrorHelper.getInternalError(InternalErrorCode.TelemetryInitializationFailed, error.message); |
10873e11digeff10 years ago | 34 | try { // try-catch for sync errors in init telemetry |
190e393cMeena Kunnathur Balakrishnan10 years ago | 35 | return this.handleErrors(telemetryError, // handleErrors for async errors in init telemetry |
10873e11digeff10 years ago | 36 | Telemetry.init("react-native", getAppVersion(), true).then(() => |
8e2d694fdigeff10 years ago | 37 | // After telemetry is initialized, we run the code. Errors in this main path are fatal so we rethrow them |
190e393cMeena Kunnathur Balakrishnan10 years ago | 38 | this.runFunction(appName, error, codeToRun, /*errorsAreFatal*/ true)), /*errorsAreFatal*/ true); |
10873e11digeff10 years ago | 39 | } catch (error) { |
898cb3c6Meena Kunnathur Balakrishnan10 years ago | 40 | Log.logError(ErrorHelper.wrapError(telemetryError, error), /*logStack*/ false); |
10873e11digeff10 years ago | 41 | throw error; |
| 42 | } | |
| 43 | } | |
| 44 | | |
190e393cMeena Kunnathur Balakrishnan10 years ago | 45 | private handleErrors(error: InternalError, resultOfCode: Q.Promise<void>, errorsAreFatal: boolean): void { |
0af0605cdigeff10 years ago | 46 | resultOfCode.done(() => { }, reason => { |
e8771a03Meena Kunnathur Balakrishnan10 years ago | 47 | const shouldLogStack = !errorsAreFatal || this.isDebugeeProcess; |
898cb3c6Meena Kunnathur Balakrishnan10 years ago | 48 | Log.logError(ErrorHelper.wrapError(error, reason), /*logStack*/ shouldLogStack); |
0af0605cdigeff10 years ago | 49 | if (errorsAreFatal) { |
8e2d694fdigeff10 years ago | 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(() => { | |
e8771a03Meena Kunnathur Balakrishnan10 years ago | 53 | if (this.isDebugeeProcess) { |
8e2d694fdigeff10 years ago | 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 | } | |
2da50044digeff10 years ago | 60 | }).done(); |
10873e11digeff10 years ago | 61 | } |
| 62 | }); | |
| 63 | } | |
| 64 | } |