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