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