microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/common/entryPointHandler.ts
69lines · 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"; |
| 10 | import {Log} from "./log/log"; | |
| 11 | import {ILogger} from "./log/loggers"; | |
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 | |
18d8ad2adigeff10 years ago | 23 | constructor(processType: ProcessType, logger?: ILogger) { |
e8771a03Meena Kunnathur Balakrishnan10 years ago | 24 | if (logger) { |
| 25 | Log.SetGlobalLogger(logger); | |
898cb3c6Meena Kunnathur Balakrishnan10 years ago | 26 | } |
e8771a03Meena Kunnathur Balakrishnan10 years ago | 27 | |
18d8ad2adigeff10 years ago | 28 | this.processType = processType; |
10873e11digeff10 years ago | 29 | } |
| 30 | | |
| 31 | /* This method should wrap any async entry points to the code, so we handle telemetry and error reporting properly */ | |
5c8365a6Artem Egorov8 years ago | 32 | public runFunction(taskName: string, error: InternalError, codeToRun: (telemetry: TelemetryGenerator) => Q.Promise<void> | void, errorsAreFatal: boolean = false): void { |
190e393cMeena Kunnathur Balakrishnan10 years ago | 33 | return this.handleErrors(error, TelemetryHelper.generate(taskName, codeToRun), /*errorsAreFatal*/ errorsAreFatal); |
10873e11digeff10 years ago | 34 | } |
| 35 | | |
41c61f9aJoshua Skelton10 years ago | 36 | // This method should wrap the entry point of the whole app, so we handle telemetry and error reporting properly |
18d8ad2adigeff10 years ago | 37 | public runApp(appName: string, getAppVersion: () => string, error: InternalError, projectRootPathOrReporterToUse: string | Telemetry.ITelemetryReporter, |
| 38 | codeToRun: () => Q.Promise<void> | void): void { | |
41c61f9aJoshua Skelton10 years ago | 39 | try { |
18d8ad2adigeff10 years ago | 40 | const appVersion = getAppVersion(); |
| 41 | const reporterToUse = typeof projectRootPathOrReporterToUse !== "string" ? <Telemetry.ITelemetryReporter>projectRootPathOrReporterToUse : null; | |
| 42 | const reporter = reporterToUse || (this.processType === ProcessType.Extension | |
| 43 | ? Telemetry.defaultTelemetryReporter(appVersion) | |
| 44 | : new ExtensionTelemetryReporter(Telemetry.appName, appVersion, Telemetry.APPINSIGHTS_INSTRUMENTATIONKEY, <string>projectRootPathOrReporterToUse)); | |
| 45 | Telemetry.init(appName, appVersion, reporter); | |
41c61f9aJoshua Skelton10 years ago | 46 | return this.runFunction(appName, error, codeToRun, true); |
10873e11digeff10 years ago | 47 | } catch (error) { |
41c61f9aJoshua Skelton10 years ago | 48 | Log.logError(error, false); |
10873e11digeff10 years ago | 49 | throw error; |
| 50 | } | |
| 51 | } | |
| 52 | | |
190e393cMeena Kunnathur Balakrishnan10 years ago | 53 | private handleErrors(error: InternalError, resultOfCode: Q.Promise<void>, errorsAreFatal: boolean): void { |
0af0605cdigeff10 years ago | 54 | resultOfCode.done(() => { }, reason => { |
18d8ad2adigeff10 years ago | 55 | const isDebugeeProcess = this.processType === ProcessType.Debugee; |
| 56 | const shouldLogStack = !errorsAreFatal || isDebugeeProcess; | |
898cb3c6Meena Kunnathur Balakrishnan10 years ago | 57 | Log.logError(ErrorHelper.wrapError(error, reason), /*logStack*/ shouldLogStack); |
b2992671Joshua Skelton10 years ago | 58 | // 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. | |
0af0605cdigeff10 years ago | 60 | if (errorsAreFatal) { |
18d8ad2adigeff10 years ago | 61 | if (isDebugeeProcess) { |
6e4d7a62Joshua Skelton10 years ago | 62 | process.exit(1); |
| 63 | } else { | |
| 64 | throw reason; | |
| 65 | } | |
10873e11digeff10 years ago | 66 | } |
| 67 | }); | |
| 68 | } | |
27710197Vladimir Kotikov8 years ago | 69 | } |