microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/common/entryPointHandler.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 {ErrorHelper} from "../common/error/errorHelper"; |
| 5 | import {ExtensionTelemetryReporter} from "../common/telemetryReporters"; |
| 6 | import {InternalError} from "../common/error/internalError"; |
| 7 | import {TelemetryHelper} from "../common/telemetryHelper"; |
| 8 | import {Telemetry} from "../common/telemetry"; |
| 9 | import {Log} from "../common/log/log"; |
| 10 | import {ILogger} from "../common/log/loggers"; |
| 11 | |
| 12 | export enum ProcessType { |
| 13 | Extension, |
| 14 | Debugee, |
| 15 | Debugger |
| 16 | } |
| 17 | |
| 18 | /* This class should we used for each entry point of the code, so we handle telemetry and error reporting properly */ |
| 19 | export class EntryPointHandler { |
| 20 | private processType: ProcessType; |
| 21 | |
| 22 | constructor(processType: ProcessType, logger?: ILogger) { |
| 23 | if (logger) { |
| 24 | Log.SetGlobalLogger(logger); |
| 25 | } |
| 26 | |
| 27 | this.processType = processType; |
| 28 | } |
| 29 | |
| 30 | /* This method should wrap any async entry points to the code, so we handle telemetry and error reporting properly */ |
| 31 | public runFunction(taskName: string, error: InternalError, codeToRun: () => Q.Promise<void> | void, errorsAreFatal: boolean = false): void { |
| 32 | return this.handleErrors(error, TelemetryHelper.generate(taskName, codeToRun), /*errorsAreFatal*/ errorsAreFatal); |
| 33 | } |
| 34 | |
| 35 | // This method should wrap the entry point of the whole app, so we handle telemetry and error reporting properly |
| 36 | public runApp(appName: string, getAppVersion: () => string, error: InternalError, projectRootPathOrReporterToUse: string | Telemetry.ITelemetryReporter, |
| 37 | codeToRun: () => Q.Promise<void> | void): void { |
| 38 | try { |
| 39 | const appVersion = getAppVersion(); |
| 40 | const reporterToUse = typeof projectRootPathOrReporterToUse !== "string" ? <Telemetry.ITelemetryReporter>projectRootPathOrReporterToUse : null; |
| 41 | const reporter = reporterToUse || (this.processType === ProcessType.Extension |
| 42 | ? Telemetry.defaultTelemetryReporter(appVersion) |
| 43 | : new ExtensionTelemetryReporter(Telemetry.appName, appVersion, Telemetry.APPINSIGHTS_INSTRUMENTATIONKEY, <string>projectRootPathOrReporterToUse)); |
| 44 | Telemetry.init(appName, appVersion, reporter); |
| 45 | return this.runFunction(appName, error, codeToRun, true); |
| 46 | } catch (error) { |
| 47 | Log.logError(error, false); |
| 48 | throw error; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | private handleErrors(error: InternalError, resultOfCode: Q.Promise<void>, errorsAreFatal: boolean): void { |
| 53 | resultOfCode.done(() => { }, reason => { |
| 54 | const isDebugeeProcess = this.processType === ProcessType.Debugee; |
| 55 | const shouldLogStack = !errorsAreFatal || isDebugeeProcess; |
| 56 | Log.logError(ErrorHelper.wrapError(error, reason), /*logStack*/ shouldLogStack); |
| 57 | // For the debugee process we don't want to throw an exception because the debugger |
| 58 | // will appear to the user if he turned on the VS Code uncaught exceptions feature. |
| 59 | if (errorsAreFatal) { |
| 60 | if (isDebugeeProcess) { |
| 61 | process.exit(1); |
| 62 | } else { |
| 63 | throw reason; |
| 64 | } |
| 65 | } |
| 66 | }); |
| 67 | } |
| 68 | } |