microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1.7.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/entryPointHandler.ts

97lines · 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 { InternalError } from "./error/internalError";
6import { TelemetryHelper, ICommandTelemetryProperties } from "./telemetryHelper";
7import { TelemetryGenerator } from "./telemetryGenerators";
8import { Telemetry } from "./telemetry";
9import { ConsoleLogger } from "../extension/log/ConsoleLogger";
10import { ILogger } from "../extension/log/LogHelper";
11
12export enum ProcessType {
13 Extension,
14 Debugee,
15 Debugger,
16}
17
18/* This class should we used for each entry point of the code, so we handle telemetry and error reporting properly */
19export class EntryPointHandler {
20 private processType: ProcessType;
21
22 constructor(processType: ProcessType, private logger: ILogger = new ConsoleLogger()) {
23 this.processType = processType;
24 }
25
26 /* This method should wrap any async entry points to the code, so we handle telemetry and error reporting properly */
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 );
41 }
42
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),
53 /*errorsAreFatal*/ errorsAreFatal,
54 );
55 }
56
57 // This method should wrap the entry point of the whole app, so we handle telemetry and error reporting properly
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> {
66 try {
67 Telemetry.init(appName, appVersion, reporter);
68 return this.runFunction(appName, error, codeToRun, true, extProps);
69 } catch (error) {
70 this.logger.error(error);
71 throw error;
72 }
73 }
74
75 private handleErrors(
76 error: InternalError,
77 resultOfCode: Promise<void>,
78 errorsAreFatal: boolean,
79 ): Promise<void> {
80 resultOfCode.catch(reason => {
81 const isDebugeeProcess = this.processType === ProcessType.Debugee;
82 const shouldLogStack = !errorsAreFatal || isDebugeeProcess;
83 this.logger.error(error.message, ErrorHelper.wrapError(error, reason), shouldLogStack);
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.
86 if (errorsAreFatal) {
87 if (isDebugeeProcess) {
88 process.exit(1);
89 } else {
90 throw reason;
91 }
92 }
93 });
94
95 return resultOfCode;
96 }
97}
98