microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
8a67e140aee58da792b1e9c1c17358a98d5a3704

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/entryPointHandler.ts

69lines · 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 {Log} from "./log/log";
11import {ILogger} from "./log/loggers";
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, logger?: ILogger) {
24 if (logger) {
25 Log.SetGlobalLogger(logger);
26 }
27
28 this.processType = processType;
29 }
30
31 /* This method should wrap any async entry points to the code, so we handle telemetry and error reporting properly */
32 public runFunction(taskName: string, error: InternalError, codeToRun: (telemetry: TelemetryGenerator) => Q.Promise<void> | void, errorsAreFatal: boolean = false): void {
33 return this.handleErrors(error, TelemetryHelper.generate(taskName, codeToRun), /*errorsAreFatal*/ errorsAreFatal);
34 }
35
36 // This method should wrap the entry point of the whole app, so we handle telemetry and error reporting properly
37 public runApp(appName: string, getAppVersion: () => string, error: InternalError, projectRootPathOrReporterToUse: string | Telemetry.ITelemetryReporter,
38 codeToRun: () => Q.Promise<void> | void): void {
39 try {
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);
46 return this.runFunction(appName, error, codeToRun, true);
47 } catch (error) {
48 Log.logError(error, false);
49 throw error;
50 }
51 }
52
53 private handleErrors(error: InternalError, resultOfCode: Q.Promise<void>, errorsAreFatal: boolean): void {
54 resultOfCode.done(() => { }, reason => {
55 const isDebugeeProcess = this.processType === ProcessType.Debugee;
56 const shouldLogStack = !errorsAreFatal || isDebugeeProcess;
57 Log.logError(ErrorHelper.wrapError(error, reason), /*logStack*/ shouldLogStack);
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.
60 if (errorsAreFatal) {
61 if (isDebugeeProcess) {
62 process.exit(1);
63 } else {
64 throw reason;
65 }
66 }
67 });
68 }
69}
70