microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.4.2

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/entryPointHandler.ts

69lines · 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
5c8365a6Artem Egorov8 years ago4import {ErrorHelper} from "./error/errorHelper";
5import {ExtensionTelemetryReporter} from "./telemetryReporters";
6import {InternalError} from "./error/internalError";
27710197Vladimir Kotikov8 years ago7import {TelemetryHelper} from "./telemetryHelper";
8import {TelemetryGenerator} from "./telemetryGenerators";
5c8365a6Artem Egorov8 years ago9import {Telemetry} from "./telemetry";
10import {Log} from "./log/log";
11import {ILogger} from "./log/loggers";
10873e11digeff10 years ago12
18d8ad2adigeff10 years ago13export enum ProcessType {
14Extension,
15Debugee,
27710197Vladimir Kotikov8 years ago16Debugger,
18d8ad2adigeff10 years ago17}
18
10873e11digeff10 years ago19/* This class should we used for each entry point of the code, so we handle telemetry and error reporting properly */
47958817digeff10 years ago20export class EntryPointHandler {
18d8ad2adigeff10 years ago21private processType: ProcessType;
10873e11digeff10 years ago22
18d8ad2adigeff10 years ago23constructor(processType: ProcessType, logger?: ILogger) {
e8771a03Meena Kunnathur Balakrishnan10 years ago24if (logger) {
25Log.SetGlobalLogger(logger);
898cb3c6Meena Kunnathur Balakrishnan10 years ago26}
e8771a03Meena Kunnathur Balakrishnan10 years ago27
18d8ad2adigeff10 years ago28this.processType = processType;
10873e11digeff10 years ago29}
30
31/* This method should wrap any async entry points to the code, so we handle telemetry and error reporting properly */
5c8365a6Artem Egorov8 years ago32public runFunction(taskName: string, error: InternalError, codeToRun: (telemetry: TelemetryGenerator) => Q.Promise<void> | void, errorsAreFatal: boolean = false): void {
190e393cMeena Kunnathur Balakrishnan10 years ago33return this.handleErrors(error, TelemetryHelper.generate(taskName, codeToRun), /*errorsAreFatal*/ errorsAreFatal);
10873e11digeff10 years ago34}
35
41c61f9aJoshua Skelton10 years ago36// This method should wrap the entry point of the whole app, so we handle telemetry and error reporting properly
18d8ad2adigeff10 years ago37public runApp(appName: string, getAppVersion: () => string, error: InternalError, projectRootPathOrReporterToUse: string | Telemetry.ITelemetryReporter,
38codeToRun: () => Q.Promise<void> | void): void {
41c61f9aJoshua Skelton10 years ago39try {
18d8ad2adigeff10 years ago40const appVersion = getAppVersion();
41const reporterToUse = typeof projectRootPathOrReporterToUse !== "string" ? <Telemetry.ITelemetryReporter>projectRootPathOrReporterToUse : null;
42const reporter = reporterToUse || (this.processType === ProcessType.Extension
43? Telemetry.defaultTelemetryReporter(appVersion)
44: new ExtensionTelemetryReporter(Telemetry.appName, appVersion, Telemetry.APPINSIGHTS_INSTRUMENTATIONKEY, <string>projectRootPathOrReporterToUse));
45Telemetry.init(appName, appVersion, reporter);
41c61f9aJoshua Skelton10 years ago46return this.runFunction(appName, error, codeToRun, true);
10873e11digeff10 years ago47} catch (error) {
41c61f9aJoshua Skelton10 years ago48Log.logError(error, false);
10873e11digeff10 years ago49throw error;
50}
51}
52
190e393cMeena Kunnathur Balakrishnan10 years ago53private handleErrors(error: InternalError, resultOfCode: Q.Promise<void>, errorsAreFatal: boolean): void {
0af0605cdigeff10 years ago54resultOfCode.done(() => { }, reason => {
18d8ad2adigeff10 years ago55const isDebugeeProcess = this.processType === ProcessType.Debugee;
56const shouldLogStack = !errorsAreFatal || isDebugeeProcess;
898cb3c6Meena Kunnathur Balakrishnan10 years ago57Log.logError(ErrorHelper.wrapError(error, reason), /*logStack*/ shouldLogStack);
b2992671Joshua Skelton10 years ago58// For the debugee process we don't want to throw an exception because the debugger
59// will appear to the user if he turned on the VS Code uncaught exceptions feature.
0af0605cdigeff10 years ago60if (errorsAreFatal) {
18d8ad2adigeff10 years ago61if (isDebugeeProcess) {
6e4d7a62Joshua Skelton10 years ago62process.exit(1);
63} else {
64throw reason;
65}
10873e11digeff10 years ago66}
67});
68}
27710197Vladimir Kotikov8 years ago69}