microsoft/vscode-react-native

Public

mirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0a68f8db56aae8352c5b9ca8062abd78cfdaa44a

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/common/entryPointHandler.ts

66lines · modecode

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
4import {ErrorHelper} from "./error/errorHelper";
5import {ExtensionTelemetryReporter} from "./telemetryReporters";
6import {InternalError} from "./error/internalError";
7import {TelemetryHelper} from "./telemetryHelper";
8import {TelemetryGenerator} from "./telemetryGenerators";
9import {Telemetry} from "./telemetry";
10import {ConsoleLogger} from "../extension/log/ConsoleLogger";
11import {ILogger} from "../extension/log/LogHelper";
12
13export enum ProcessType {
14 Extension,
15 Debugee,
16 Debugger,
17}
18
19/* This class should we used for each entry point of the code, so we handle telemetry and error reporting properly */
20export class EntryPointHandler {
21 private processType: ProcessType;
22
23 constructor(processType: ProcessType, private logger: ILogger = new ConsoleLogger()) {
24
25 this.processType = processType;
26 }
27
28 /* This method should wrap any async entry points to the code, so we handle telemetry and error reporting properly */
29 public runFunction(taskName: string, error: InternalError, codeToRun: (telemetry: TelemetryGenerator) => Q.Promise<void> | void, errorsAreFatal: boolean = false): void {
30 return this.handleErrors(error, TelemetryHelper.generate(taskName, codeToRun), /*errorsAreFatal*/ errorsAreFatal);
31 }
32
33 // This method should wrap the entry point of the whole app, so we handle telemetry and error reporting properly
34 public runApp(appName: string, getAppVersion: () => string, error: InternalError, projectRootPathOrReporterToUse: string | Telemetry.ITelemetryReporter,
35 codeToRun: () => Q.Promise<void> | void): void {
36 try {
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);
43 return this.runFunction(appName, error, codeToRun, true);
44 } catch (error) {
45 this.logger.error(error);
46 throw error;
47 }
48 }
49
50 private handleErrors(error: InternalError, resultOfCode: Q.Promise<void>, errorsAreFatal: boolean): void {
51 resultOfCode.done(() => { }, reason => {
52 const isDebugeeProcess = this.processType === ProcessType.Debugee;
53 const shouldLogStack = !errorsAreFatal || isDebugeeProcess;
54 this.logger.error(error.message, ErrorHelper.wrapError(error, reason), shouldLogStack);
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.
57 if (errorsAreFatal) {
58 if (isDebugeeProcess) {
59 process.exit(1);
60 } else {
61 throw reason;
62 }
63 }
64 });
65 }
66}
67