microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.1.2

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/entryPointHandler.ts

64lines · 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
190e393cMeena Kunnathur Balakrishnan10 years ago4
5import {ErrorHelper} from "../common/error/errorHelper";
6import {InternalError} from "../common/error/internalError";
7import {InternalErrorCode} from "../common/error/internalErrorCode";
8e2d694fdigeff10 years ago8import {TelemetryHelper} from "../common/telemetryHelper";
9import {Telemetry} from "../common/telemetry";
190e393cMeena Kunnathur Balakrishnan10 years ago10import {Log} from "../common/log/log";
e8771a03Meena Kunnathur Balakrishnan10 years ago11import {ILogger} from "../common/log/loggers";
10873e11digeff10 years ago12
13/* This class should we used for each entry point of the code, so we handle telemetry and error reporting properly */
47958817digeff10 years ago14export class EntryPointHandler {
e8771a03Meena Kunnathur Balakrishnan10 years ago15private isDebugeeProcess: boolean;
10873e11digeff10 years ago16
e8771a03Meena Kunnathur Balakrishnan10 years ago17constructor(isDebugeeProcess: boolean = false, logger?: ILogger) {
18if (logger) {
19Log.SetGlobalLogger(logger);
898cb3c6Meena Kunnathur Balakrishnan10 years ago20}
e8771a03Meena Kunnathur Balakrishnan10 years ago21
22this.isDebugeeProcess = isDebugeeProcess;
10873e11digeff10 years ago23}
24
ac1bd6f1digeff10 years ago25
10873e11digeff10 years ago26/* This method should wrap any async entry points to the code, so we handle telemetry and error reporting properly */
190e393cMeena Kunnathur Balakrishnan10 years ago27public runFunction(taskName: string, error: InternalError, codeToRun: () => Q.Promise<void> | void, errorsAreFatal: boolean = false): void {
28return this.handleErrors(error, TelemetryHelper.generate(taskName, codeToRun), /*errorsAreFatal*/ errorsAreFatal);
10873e11digeff10 years ago29}
30
31/* This method should wrap the entry point of the whole app, so we handle telemetry and error reporting properly */
190e393cMeena Kunnathur Balakrishnan10 years ago32public runApp(appName: string, getAppVersion: () => string, error: InternalError, codeToRun: () => Q.Promise<void>): void {
a289475bMeena Kunnathur Balakrishnan10 years ago33let telemetryError = ErrorHelper.getInternalError(InternalErrorCode.TelemetryInitializationFailed, error.message);
10873e11digeff10 years ago34try { // try-catch for sync errors in init telemetry
190e393cMeena Kunnathur Balakrishnan10 years ago35return this.handleErrors(telemetryError, // handleErrors for async errors in init telemetry
10873e11digeff10 years ago36Telemetry.init("react-native", getAppVersion(), true).then(() =>
8e2d694fdigeff10 years ago37// After telemetry is initialized, we run the code. Errors in this main path are fatal so we rethrow them
190e393cMeena Kunnathur Balakrishnan10 years ago38this.runFunction(appName, error, codeToRun, /*errorsAreFatal*/ true)), /*errorsAreFatal*/ true);
10873e11digeff10 years ago39} catch (error) {
898cb3c6Meena Kunnathur Balakrishnan10 years ago40Log.logError(ErrorHelper.wrapError(telemetryError, error), /*logStack*/ false);
10873e11digeff10 years ago41throw error;
42}
43}
44
190e393cMeena Kunnathur Balakrishnan10 years ago45private handleErrors(error: InternalError, resultOfCode: Q.Promise<void>, errorsAreFatal: boolean): void {
0af0605cdigeff10 years ago46resultOfCode.done(() => { }, reason => {
e8771a03Meena Kunnathur Balakrishnan10 years ago47const shouldLogStack = !errorsAreFatal || this.isDebugeeProcess;
898cb3c6Meena Kunnathur Balakrishnan10 years ago48Log.logError(ErrorHelper.wrapError(error, reason), /*logStack*/ shouldLogStack);
0af0605cdigeff10 years ago49if (errorsAreFatal) {
8e2d694fdigeff10 years ago50/* The process is likely going to exit if errors are fatal, so we first
51send the telemetry, and then we exit or rethrow the exception */
52Telemetry.sendPendingData().finally(() => {
e8771a03Meena Kunnathur Balakrishnan10 years ago53if (this.isDebugeeProcess) {
8e2d694fdigeff10 years ago54/* HACK: For the debugee process we don't want to throw an exception because the debugger
55will appear to the user if he turned on the VS Code uncaught exceptions feature. */
56process.exit(1);
57} else {
58throw reason;
59}
2da50044digeff10 years ago60}).done();
10873e11digeff10 years ago61}
62});
63}
64}