microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/common/entryPointHandler.ts
57lines · 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 {TelemetryHelper} from "../common/telemetryHelper"; |
| 5 | import {Telemetry} from "../common/telemetry"; |
| 6 | import {Log} from "../common/log"; |
| 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 */ |
| 10 | export class EntryPointHandler { |
| 11 | private outputChannel: OutputChannel; |
| 12 | |
| 13 | constructor(outputChannel?: OutputChannel) { |
| 14 | this.outputChannel = outputChannel; |
| 15 | } |
| 16 | |
| 17 | |
| 18 | /* This method should wrap any async entry points to the code, so we handle telemetry and error reporting properly */ |
| 19 | public runFunction(taskName: string, errorDescription: string, codeToRun: () => Q.Promise<void> | void, errorsAreFatal: boolean = false): void { |
| 20 | return this.handleErrors(errorDescription, TelemetryHelper.generate(taskName, codeToRun), /*errorsAreFatal*/ errorsAreFatal); |
| 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(() => |
| 29 | // After telemetry is initialized, we run the code. Errors in this main path are fatal so we rethrow them |
| 30 | this.runFunction(appName, errorDescription, codeToRun, /*errorsAreFatal*/ true)), /*errorsAreFatal*/ true); |
| 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 | |
| 37 | private handleErrors(errorDescription: string, resultOfCode: Q.Promise<void>, errorsAreFatal: boolean): void { |
| 38 | const isDebugeeProcess = !this.outputChannel; |
| 39 | resultOfCode.done(() => { }, reason => { |
| 40 | const shouldLogStack = !errorsAreFatal || isDebugeeProcess; |
| 41 | Log.logError(errorDescription, reason, this.outputChannel, /*logStack*/ shouldLogStack); |
| 42 | if (errorsAreFatal) { |
| 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(() => { |
| 46 | if (isDebugeeProcess) { |
| 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 | } |
| 53 | }).done(); |
| 54 | } |
| 55 | }); |
| 56 | } |
| 57 | } |