microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
db80cd4e10b83524bc77c15018effab72cbabeb8

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/entryPointHandler.ts

57lines · modeblame

8ee905e8digeff10 years ago1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
8e2d694fdigeff10 years ago4import {TelemetryHelper} from "../common/telemetryHelper";
5import {Telemetry} from "../common/telemetry";
252ec333digeff10 years ago6import {Log} from "../common/log";
10873e11digeff10 years ago7import {OutputChannel} from "vscode";
8
9/* This class should we used for each entry point of the code, so we handle telemetry and error reporting properly */
47958817digeff10 years ago10export class EntryPointHandler {
10873e11digeff10 years ago11private outputChannel: OutputChannel;
12
13constructor(outputChannel?: OutputChannel) {
14this.outputChannel = outputChannel;
15}
16
ac1bd6f1digeff10 years ago17
10873e11digeff10 years ago18/* This method should wrap any async entry points to the code, so we handle telemetry and error reporting properly */
ef24cc06digeff10 years ago19public runFunction(taskName: string, errorDescription: string, codeToRun: () => Q.Promise<void> | void, errorsAreFatal: boolean = false): void {
0af0605cdigeff10 years ago20return this.handleErrors(errorDescription, TelemetryHelper.generate(taskName, codeToRun), /*errorsAreFatal*/ errorsAreFatal);
10873e11digeff10 years ago21}
22
23/* This method should wrap the entry point of the whole app, so we handle telemetry and error reporting properly */
24public runApp(appName: string, getAppVersion: () => string, errorDescription: string, codeToRun: () => Q.Promise<void>): void {
25const telemetryErrorDescription = `${errorDescription}. Couldn't initialize telemetry`;
26try { // try-catch for sync errors in init telemetry
27return this.handleErrors(telemetryErrorDescription, // handleErrors for async errors in init telemetry
28Telemetry.init("react-native", getAppVersion(), true).then(() =>
8e2d694fdigeff10 years ago29// After telemetry is initialized, we run the code. Errors in this main path are fatal so we rethrow them
ac1bd6f1digeff10 years ago30this.runFunction(appName, errorDescription, codeToRun, /*errorsAreFatal*/ true)), /*errorsAreFatal*/ true);
10873e11digeff10 years ago31} catch (error) {
32Log.logError(telemetryErrorDescription, error, this.outputChannel, /*logStack*/ false); // Print the error and re-throw the exception
33throw error;
34}
35}
36
0af0605cdigeff10 years ago37private handleErrors(errorDescription: string, resultOfCode: Q.Promise<void>, errorsAreFatal: boolean): void {
a7d36f2fdigeff10 years ago38const isDebugeeProcess = !this.outputChannel;
0af0605cdigeff10 years ago39resultOfCode.done(() => { }, reason => {
40const shouldLogStack = !errorsAreFatal || isDebugeeProcess;
8e2d694fdigeff10 years ago41Log.logError(errorDescription, reason, this.outputChannel, /*logStack*/ shouldLogStack);
0af0605cdigeff10 years ago42if (errorsAreFatal) {
8e2d694fdigeff10 years ago43/* The process is likely going to exit if errors are fatal, so we first
44send the telemetry, and then we exit or rethrow the exception */
45Telemetry.sendPendingData().finally(() => {
a7d36f2fdigeff10 years ago46if (isDebugeeProcess) {
8e2d694fdigeff10 years ago47/* HACK: For the debugee process we don't want to throw an exception because the debugger
48will appear to the user if he turned on the VS Code uncaught exceptions feature. */
49process.exit(1);
50} else {
51throw reason;
52}
2da50044digeff10 years ago53}).done();
10873e11digeff10 years ago54}
55});
56}
57}