microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
14ebf4e6fdee5f69a41d9a7deea4dc164dd28b7e

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/entryPointHandler.ts

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