microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
881dd8be6de113dd9a45da8f9211f47d16efaf25

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/entryPointHandler.ts

56lines · 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
4
5import {ErrorHelper} from "../common/error/errorHelper";
6import {InternalError} from "../common/error/internalError";
7import {TelemetryHelper} from "../common/telemetryHelper";
8import {Telemetry} from "../common/telemetry";
9import {Log} from "../common/log/log";
10import {ILogger} from "../common/log/loggers";
11
12/* This class should we used for each entry point of the code, so we handle telemetry and error reporting properly */
13export class EntryPointHandler {
14 private isDebugeeProcess: boolean;
15
16 constructor(isDebugeeProcess: boolean = false, logger?: ILogger) {
17 if (logger) {
18 Log.SetGlobalLogger(logger);
19 }
20
21 this.isDebugeeProcess = isDebugeeProcess;
22 }
23
24
25 /* This method should wrap any async entry points to the code, so we handle telemetry and error reporting properly */
26 public runFunction(taskName: string, error: InternalError, codeToRun: () => Q.Promise<void> | void, errorsAreFatal: boolean = false): void {
27 return this.handleErrors(error, TelemetryHelper.generate(taskName, codeToRun), /*errorsAreFatal*/ errorsAreFatal);
28 }
29
30 // This method should wrap the entry point of the whole app, so we handle telemetry and error reporting properly
31 public runApp(appName: string, getAppVersion: () => string, error: InternalError, codeToRun: () => Q.Promise<void>): void {
32 try {
33 Telemetry.init(appName, getAppVersion(), {isExtensionProcess: !this.isDebugeeProcess});
34 return this.runFunction(appName, error, codeToRun, true);
35 } catch (error) {
36 Log.logError(error, false);
37 throw error;
38 }
39 }
40
41 private handleErrors(error: InternalError, resultOfCode: Q.Promise<void>, errorsAreFatal: boolean): void {
42 resultOfCode.done(() => { }, reason => {
43 const shouldLogStack = !errorsAreFatal || this.isDebugeeProcess;
44 Log.logError(ErrorHelper.wrapError(error, reason), /*logStack*/ shouldLogStack);
45 // For the debugee process we don't want to throw an exception because the debugger
46 // will appear to the user if he turned on the VS Code uncaught exceptions feature.
47 if (errorsAreFatal) {
48 if (this.isDebugeeProcess) {
49 process.exit(1);
50 } else {
51 throw reason;
52 }
53 }
54 });
55 }
56}