microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
dev/v-zhenyuan/update-parameters

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/entryPointHandler.ts

100lines · 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 { ConsoleLogger } from "../extension/log/ConsoleLogger";
5import { ILogger } from "../extension/log/LogHelper";
6import { ErrorHelper } from "./error/errorHelper";
7import { InternalError } from "./error/internalError";
8import { TelemetryHelper, ICommandTelemetryProperties } from "./telemetryHelper";
9import { TelemetryGenerator } from "./telemetryGenerators";
10import { Telemetry } from "./telemetry";
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,
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(
71 error instanceof Error ? error.message : String(error),
72 error instanceof Error ? error : undefined,
73 );
74 throw error;
75 }
76 }
77
78 private handleErrors(
79 error: InternalError,
80 resultOfCode: Promise<void>,
81 errorsAreFatal: boolean,
82 ): Promise<void> {
83 resultOfCode.catch(reason => {
84 const isDebugeeProcess = this.processType === ProcessType.Debugee;
85 const shouldLogStack = !errorsAreFatal || isDebugeeProcess;
86 this.logger.error(error.message, ErrorHelper.wrapError(error, reason), shouldLogStack);
87 // For the debugee process we don't want to throw an exception because the debugger
88 // will appear to the user if he turned on the VS Code uncaught exceptions feature.
89 if (errorsAreFatal) {
90 if (isDebugeeProcess) {
91 process.exit(1);
92 } else {
93 throw reason;
94 }
95 }
96 });
97
98 return resultOfCode;
99 }
100}
101