microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
a289475be0da2ee07b9b056760f2f0a3076877b2

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/entryPointHandler.ts

61lines · 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 {InternalErrorCode} from "../common/error/internalErrorCode";
8import {TelemetryHelper} from "../common/telemetryHelper";
9import {Telemetry} from "../common/telemetry";
10import {Log} from "../common/log/log";
11import {OutputChannel} from "vscode";
12
13/* This class should we used for each entry point of the code, so we handle telemetry and error reporting properly */
14export class EntryPointHandler {
15 private outputChannel: OutputChannel;
16
17 constructor(outputChannel?: OutputChannel) {
18 this.outputChannel = outputChannel;
19 }
20
21
22 /* This method should wrap any async entry points to the code, so we handle telemetry and error reporting properly */
23 public runFunction(taskName: string, error: InternalError, codeToRun: () => Q.Promise<void> | void, errorsAreFatal: boolean = false): void {
24 return this.handleErrors(error, TelemetryHelper.generate(taskName, codeToRun), /*errorsAreFatal*/ errorsAreFatal);
25 }
26
27 /* This method should wrap the entry point of the whole app, so we handle telemetry and error reporting properly */
28 public runApp(appName: string, getAppVersion: () => string, error: InternalError, codeToRun: () => Q.Promise<void>): void {
29 let telemetryError = ErrorHelper.getInternalError(InternalErrorCode.TelemetryInitializationFailed, error.message);
30 try { // try-catch for sync errors in init telemetry
31 return this.handleErrors(telemetryError, // handleErrors for async errors in init telemetry
32 Telemetry.init("react-native", getAppVersion(), true).then(() =>
33 // After telemetry is initialized, we run the code. Errors in this main path are fatal so we rethrow them
34 this.runFunction(appName, error, codeToRun, /*errorsAreFatal*/ true)), /*errorsAreFatal*/ true);
35 } catch (error) {
36 Log.logError(ErrorHelper.wrapError(telemetryError, error), this.outputChannel, /*logStack*/ false);
37 throw error;
38 }
39 }
40
41 private handleErrors(error: InternalError, resultOfCode: Q.Promise<void>, errorsAreFatal: boolean): void {
42 const isDebugeeProcess = !this.outputChannel;
43 resultOfCode.done(() => { }, reason => {
44 const shouldLogStack = !errorsAreFatal || isDebugeeProcess;
45 Log.logError(ErrorHelper.wrapError(error, reason), this.outputChannel, /*logStack*/ shouldLogStack);
46 if (errorsAreFatal) {
47 /* The process is likely going to exit if errors are fatal, so we first
48 send the telemetry, and then we exit or rethrow the exception */
49 Telemetry.sendPendingData().finally(() => {
50 if (isDebugeeProcess) {
51 /* HACK: For the debugee process we don't want to throw an exception because the debugger
52 will appear to the user if he turned on the VS Code uncaught exceptions feature. */
53 process.exit(1);
54 } else {
55 throw reason;
56 }
57 }).done();
58 }
59 });
60 }
61}