microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/common/entryPoint.ts
68lines · 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 EntryPoint { |
| 11 | private outputChannel: OutputChannel; |
| 12 | |
| 13 | constructor(outputChannel?: OutputChannel) { |
| 14 | this.outputChannel = outputChannel; |
| 15 | } |
| 16 | |
| 17 | /* This method should wrap any async entry points to the code, so we handle telemetry and error reporting properly */ |
| 18 | public runCode(taskName: string, errorDescription: string, codeToRun: () => Q.Promise<void>, areErrorsFatal: boolean): void { |
| 19 | return this.handleErrors(errorDescription, TelemetryHelper.generate(taskName, codeToRun), /*areErrorsFatal*/ areErrorsFatal); |
| 20 | } |
| 21 | |
| 22 | /* This method should wrap any 100% sync entry points to the code, so we handle telemetry and error reporting properly */ |
| 23 | public runSyncCode(taskName: string, errorDescription: string, codeToRun: () => void): void { |
| 24 | try { |
| 25 | TelemetryHelper.sendSimpleEvent(taskName + ".starting"); // We call sendSimpleEvent because generate is async only |
| 26 | codeToRun(); |
| 27 | TelemetryHelper.sendSimpleEvent(taskName + ".succesfull"); |
| 28 | } catch (error) { |
| 29 | Log.logError(errorDescription, error, this.outputChannel, /*logStack*/ true); |
| 30 | TelemetryHelper.sendSimpleEvent(taskName + ".failed"); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | /* This method should wrap the entry point of the whole app, so we handle telemetry and error reporting properly */ |
| 35 | public runApp(appName: string, getAppVersion: () => string, errorDescription: string, codeToRun: () => Q.Promise<void>): void { |
| 36 | const telemetryErrorDescription = `${errorDescription}. Couldn't initialize telemetry`; |
| 37 | try { // try-catch for sync errors in init telemetry |
| 38 | return this.handleErrors(telemetryErrorDescription, // handleErrors for async errors in init telemetry |
| 39 | Telemetry.init("react-native", getAppVersion(), true).then(() => |
| 40 | // After telemetry is initialized, we run the code. Errors in this main path are fatal so we rethrow them |
| 41 | this.runCode(appName, errorDescription, codeToRun, /*areErrorsFatal*/ true)), /*areErrorsFatal*/ true); |
| 42 | } catch (error) { |
| 43 | Log.logError(telemetryErrorDescription, error, this.outputChannel, /*logStack*/ false); // Print the error and re-throw the exception |
| 44 | throw error; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | private handleErrors(errorDescription: string, codeToRun: Q.Promise<void>, areErrorsFatal: boolean): void { |
| 49 | const isDebugeedProcess = !this.outputChannel; |
| 50 | codeToRun.done(() => { }, reason => { |
| 51 | const shouldLogStack = !areErrorsFatal || isDebugeedProcess; |
| 52 | Log.logError(errorDescription, reason, this.outputChannel, /*logStack*/ shouldLogStack); |
| 53 | if (areErrorsFatal) { |
| 54 | /* The process is likely going to exit if errors are fatal, so we first |
| 55 | send the telemetry, and then we exit or rethrow the exception */ |
| 56 | Telemetry.sendPendingData().finally(() => { |
| 57 | if (isDebugeedProcess) { |
| 58 | /* HACK: For the debugee process we don't want to throw an exception because the debugger |
| 59 | will appear to the user if he turned on the VS Code uncaught exceptions feature. */ |
| 60 | process.exit(1); |
| 61 | } else { |
| 62 | throw reason; |
| 63 | } |
| 64 | }); |
| 65 | } |
| 66 | }); |
| 67 | } |
| 68 | } |