microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/common/entryPointHandler.ts
57lines · 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 | | |
8e2d694fdigeff10 years ago | 4 | import {TelemetryHelper} from "../common/telemetryHelper"; |
| 5 | import {Telemetry} from "../common/telemetry"; | |
252ec333digeff10 years ago | 6 | import {Log} from "../common/log"; |
10873e11digeff10 years ago | 7 | import {OutputChannel} from "vscode"; |
| 8 | | |
| 9 | /* This class should we used for each entry point of the code, so we handle telemetry and error reporting properly */ | |
47958817digeff10 years ago | 10 | export class EntryPointHandler { |
10873e11digeff10 years ago | 11 | private outputChannel: OutputChannel; |
| 12 | | |
| 13 | constructor(outputChannel?: OutputChannel) { | |
| 14 | this.outputChannel = outputChannel; | |
| 15 | } | |
| 16 | | |
ac1bd6f1digeff10 years ago | 17 | |
10873e11digeff10 years ago | 18 | /* This method should wrap any async entry points to the code, so we handle telemetry and error reporting properly */ |
ef24cc06digeff10 years ago | 19 | public runFunction(taskName: string, errorDescription: string, codeToRun: () => Q.Promise<void> | void, errorsAreFatal: boolean = false): void { |
0af0605cdigeff10 years ago | 20 | return this.handleErrors(errorDescription, TelemetryHelper.generate(taskName, codeToRun), /*errorsAreFatal*/ errorsAreFatal); |
10873e11digeff10 years ago | 21 | } |
| 22 | | |
| 23 | /* This method should wrap the entry point of the whole app, so we handle telemetry and error reporting properly */ | |
| 24 | public runApp(appName: string, getAppVersion: () => string, errorDescription: string, codeToRun: () => Q.Promise<void>): void { | |
| 25 | const telemetryErrorDescription = `${errorDescription}. Couldn't initialize telemetry`; | |
| 26 | try { // try-catch for sync errors in init telemetry | |
| 27 | return this.handleErrors(telemetryErrorDescription, // handleErrors for async errors in init telemetry | |
| 28 | Telemetry.init("react-native", getAppVersion(), true).then(() => | |
8e2d694fdigeff10 years ago | 29 | // After telemetry is initialized, we run the code. Errors in this main path are fatal so we rethrow them |
ac1bd6f1digeff10 years ago | 30 | this.runFunction(appName, errorDescription, codeToRun, /*errorsAreFatal*/ true)), /*errorsAreFatal*/ true); |
10873e11digeff10 years ago | 31 | } catch (error) { |
| 32 | Log.logError(telemetryErrorDescription, error, this.outputChannel, /*logStack*/ false); // Print the error and re-throw the exception | |
| 33 | throw error; | |
| 34 | } | |
| 35 | } | |
| 36 | | |
0af0605cdigeff10 years ago | 37 | private handleErrors(errorDescription: string, resultOfCode: Q.Promise<void>, errorsAreFatal: boolean): void { |
a7d36f2fdigeff10 years ago | 38 | const isDebugeeProcess = !this.outputChannel; |
0af0605cdigeff10 years ago | 39 | resultOfCode.done(() => { }, reason => { |
| 40 | const shouldLogStack = !errorsAreFatal || isDebugeeProcess; | |
8e2d694fdigeff10 years ago | 41 | Log.logError(errorDescription, reason, this.outputChannel, /*logStack*/ shouldLogStack); |
0af0605cdigeff10 years ago | 42 | if (errorsAreFatal) { |
8e2d694fdigeff10 years ago | 43 | /* The process is likely going to exit if errors are fatal, so we first |
| 44 | send the telemetry, and then we exit or rethrow the exception */ | |
| 45 | Telemetry.sendPendingData().finally(() => { | |
a7d36f2fdigeff10 years ago | 46 | if (isDebugeeProcess) { |
8e2d694fdigeff10 years ago | 47 | /* HACK: For the debugee process we don't want to throw an exception because the debugger |
| 48 | will appear to the user if he turned on the VS Code uncaught exceptions feature. */ | |
| 49 | process.exit(1); | |
| 50 | } else { | |
| 51 | throw reason; | |
| 52 | } | |
2da50044digeff10 years ago | 53 | }).done(); |
10873e11digeff10 years ago | 54 | } |
| 55 | }); | |
| 56 | } | |
| 57 | } |