microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/common/entryPointHandler.ts
97lines · 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 | | |
09f6024fHeniker4 years ago | 4 | import { ConsoleLogger } from "../extension/log/ConsoleLogger"; |
| 5 | import { ILogger } from "../extension/log/LogHelper"; | |
34472878RedMickey5 years ago | 6 | import { ErrorHelper } from "./error/errorHelper"; |
| 7 | import { InternalError } from "./error/internalError"; | |
| 8 | import { TelemetryHelper, ICommandTelemetryProperties } from "./telemetryHelper"; | |
| 9 | import { TelemetryGenerator } from "./telemetryGenerators"; | |
| 10 | import { Telemetry } from "./telemetry"; | |
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()) { |
18d8ad2adigeff10 years ago | 23 | this.processType = processType; |
10873e11digeff10 years ago | 24 | } |
| 25 | | |
| 26 | /* This method should wrap any async entry points to the code, so we handle telemetry and error reporting properly */ | |
34472878RedMickey5 years ago | 27 | public runFunction( |
| 28 | taskName: string, | |
| 29 | error: InternalError, | |
| 30 | codeToRun: (telemetry: TelemetryGenerator) => Promise<void> | void, | |
| 31 | errorsAreFatal: boolean = false, | |
| 32 | extProps?: ICommandTelemetryProperties, | |
| 33 | ): Promise<void> { | |
| 34 | return this.runFunctionWExtProps( | |
| 35 | taskName, | |
| 36 | extProps || {}, | |
| 37 | error, | |
| 38 | codeToRun, | |
| 39 | errorsAreFatal, | |
| 40 | ); | |
031832ffArtem Egorov7 years ago | 41 | } |
| 42 | | |
34472878RedMickey5 years ago | 43 | public runFunctionWExtProps( |
| 44 | taskName: string, | |
| 45 | extProps: ICommandTelemetryProperties, | |
| 46 | error: InternalError, | |
| 47 | codeToRun: (telemetry: TelemetryGenerator) => Promise<void> | void, | |
| 48 | errorsAreFatal: boolean = false, | |
| 49 | ): Promise<void> { | |
| 50 | return this.handleErrors( | |
| 51 | error, | |
| 52 | TelemetryHelper.generate(taskName, extProps, codeToRun), | |
36a21645Heniker4 years ago | 53 | errorsAreFatal, |
34472878RedMickey5 years ago | 54 | ); |
10873e11digeff10 years ago | 55 | } |
| 56 | | |
41c61f9aJoshua Skelton10 years ago | 57 | // This method should wrap the entry point of the whole app, so we handle telemetry and error reporting properly |
34472878RedMickey5 years ago | 58 | public runApp( |
| 59 | appName: string, | |
| 60 | appVersion: string, | |
| 61 | error: InternalError, | |
| 62 | reporter: Telemetry.ITelemetryReporter, | |
| 63 | codeToRun: () => Promise<void> | void, | |
| 64 | extProps?: ICommandTelemetryProperties, | |
| 65 | ): Promise<void> { | |
41c61f9aJoshua Skelton10 years ago | 66 | try { |
18d8ad2adigeff10 years ago | 67 | Telemetry.init(appName, appVersion, reporter); |
a432afe1Yuri Skorokhodov7 years ago | 68 | return this.runFunction(appName, error, codeToRun, true, extProps); |
10873e11digeff10 years ago | 69 | } catch (error) { |
0a68f8dbArtem Egorov8 years ago | 70 | this.logger.error(error); |
10873e11digeff10 years ago | 71 | throw error; |
| 72 | } | |
| 73 | } | |
| 74 | | |
34472878RedMickey5 years ago | 75 | private handleErrors( |
| 76 | error: InternalError, | |
| 77 | resultOfCode: Promise<void>, | |
| 78 | errorsAreFatal: boolean, | |
| 79 | ): Promise<void> { | |
| 80 | resultOfCode.catch(reason => { | |
18d8ad2adigeff10 years ago | 81 | const isDebugeeProcess = this.processType === ProcessType.Debugee; |
| 82 | const shouldLogStack = !errorsAreFatal || isDebugeeProcess; | |
0a68f8dbArtem Egorov8 years ago | 83 | this.logger.error(error.message, ErrorHelper.wrapError(error, reason), shouldLogStack); |
b2992671Joshua Skelton10 years ago | 84 | // For the debugee process we don't want to throw an exception because the debugger |
| 85 | // will appear to the user if he turned on the VS Code uncaught exceptions feature. | |
0af0605cdigeff10 years ago | 86 | if (errorsAreFatal) { |
18d8ad2adigeff10 years ago | 87 | if (isDebugeeProcess) { |
6e4d7a62Joshua Skelton10 years ago | 88 | process.exit(1); |
| 89 | } else { | |
| 90 | throw reason; | |
| 91 | } | |
10873e11digeff10 years ago | 92 | } |
| 93 | }); | |
1950c5e9Artem Egorov8 years ago | 94 | |
| 95 | return resultOfCode; | |
10873e11digeff10 years ago | 96 | } |
27710197Vladimir Kotikov8 years ago | 97 | } |