microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
377a189c0ee91d327ee248d1f6d7e996f4f7a4e2

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/entryPointHandler.ts

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