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