microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
d55f3c22ee18a37c605867c8bf588451292bd24e

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/entryPointHandler.ts

65lines · 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
24 this.processType = processType;
25 }
26
27 /* This method should wrap any async entry points to the code, so we handle telemetry and error reporting properly */
28 public runFunction(taskName: string, error: InternalError, codeToRun: (telemetry: TelemetryGenerator) => Promise<void> | void, errorsAreFatal: boolean = false, extProps?: ICommandTelemetryProperties): Promise<void> {
29 return this.runFunctionWExtProps(taskName, extProps || {}, error, codeToRun, errorsAreFatal);
30 }
31
32 public runFunctionWExtProps(taskName: string, extProps: ICommandTelemetryProperties, error: InternalError, codeToRun: (telemetry: TelemetryGenerator) => Promise<void> | void, errorsAreFatal: boolean = false): Promise<void> {
33 return this.handleErrors(error, TelemetryHelper.generate(taskName, extProps, 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, appVersion: string, error: InternalError, reporter: Telemetry.ITelemetryReporter, codeToRun: () => Promise<void> | void, extProps?: ICommandTelemetryProperties): Promise<void> {
38 try {
39 Telemetry.init(appName, appVersion, reporter);
40 return this.runFunction(appName, error, codeToRun, true, extProps);
41 } catch (error) {
42 this.logger.error(error);
43 throw error;
44 }
45 }
46
47 private handleErrors(error: InternalError, resultOfCode: Promise<void>, errorsAreFatal: boolean): Promise<void> {
48 resultOfCode.then(() => { }, reason => {
49 const isDebugeeProcess = this.processType === ProcessType.Debugee;
50 const shouldLogStack = !errorsAreFatal || isDebugeeProcess;
51 this.logger.error(error.message, ErrorHelper.wrapError(error, reason), shouldLogStack);
52 // For the debugee process we don't want to throw an exception because the debugger
53 // will appear to the user if he turned on the VS Code uncaught exceptions feature.
54 if (errorsAreFatal) {
55 if (isDebugeeProcess) {
56 process.exit(1);
57 } else {
58 throw reason;
59 }
60 }
61 });
62
63 return resultOfCode;
64 }
65}
66