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