From ec8c3bc26d2a5ebe0622122bbbec2a3a215bf302 Mon Sep 17 00:00:00 2001 From: "Hung-Han (Henry) Chen" Date: Mon, 12 Jul 2021 12:43:14 +0300 Subject: [PATCH] Remove try-catch for Sentry.init (exception should be caught by lens-ide tests, not in runtime), remove isValidSentryDsn/isValidSentryDsn(), use sentryDsn string to determine if preference UI should show Signed-off-by: Hung-Han (Henry) Chen --- src/common/sentry.ts | 81 ++++++++----------- src/common/utils/index.ts | 1 - src/common/utils/isValidSentryDsn.ts | 38 --------- src/common/vars.ts | 2 - .../components/+preferences/preferences.tsx | 4 +- 5 files changed, 37 insertions(+), 89 deletions(-) delete mode 100644 src/common/utils/isValidSentryDsn.ts diff --git a/src/common/sentry.ts b/src/common/sentry.ts index 68453b4406..50cf847673 100644 --- a/src/common/sentry.ts +++ b/src/common/sentry.ts @@ -21,7 +21,7 @@ import { CaptureConsole, Dedupe, Offline } from "@sentry/integrations"; import * as Sentry from "@sentry/electron"; -import { sentryDsn, sentryDsnIsValid, isProduction } from "./vars"; +import { sentryDsn, isProduction } from "./vars"; import { UserStore } from "./user-store"; import logger from "../main/logger"; @@ -40,55 +40,44 @@ function mapProcessName(processType: string) { * Initialize Sentry for the current process so to send errors for debugging. */ export function SentryInit() { - try { - const processName = mapProcessName(process.type); + const processName = mapProcessName(process.type); - Sentry.init({ - beforeSend: (event) => { - // default to false, in case instance of UserStore is not created (yet) - let allowErrorReporting = false; + Sentry.init({ + beforeSend: (event) => { + // default to false, in case instance of UserStore is not created (yet) + let allowErrorReporting = false; - try { - allowErrorReporting = UserStore.getInstance()?.allowErrorReporting; - } catch { - // ignore the TypeError throw by UserStore.getInstance() - } + try { + allowErrorReporting = UserStore.getInstance()?.allowErrorReporting; + } catch { + // ignore the TypeError throw by UserStore.getInstance() + } - if (allowErrorReporting) { - return event; - } + if (allowErrorReporting) { + return event; + } - logger.info(`🔒 [SENTRY-BEFORE-SEND-HOOK]: allowErrorReporting: ${allowErrorReporting}. Sentry event is caught but not sent to server.`); - logger.info("🔒 [SENTRY-BEFORE-SEND-HOOK]: === START OF SENTRY EVENT ==="); - logger.info(event); - logger.info("🔒 [SENTRY-BEFORE-SEND-HOOK]: === END OF SENTRY EVENT ==="); + logger.info(`🔒 [SENTRY-BEFORE-SEND-HOOK]: allowErrorReporting: ${allowErrorReporting}. Sentry event is caught but not sent to server.`); + logger.info("🔒 [SENTRY-BEFORE-SEND-HOOK]: === START OF SENTRY EVENT ==="); + logger.info(event); + logger.info("🔒 [SENTRY-BEFORE-SEND-HOOK]: === END OF SENTRY EVENT ==="); - // if return null, the event won't be sent - // ref https://github.com/getsentry/sentry-javascript/issues/2039 - return null; - }, - dsn: sentryDsn, - integrations: [ - new CaptureConsole({ levels: ["error"] }), - new Dedupe(), - new Offline() - ], - initialScope: { - tags: { + // if return null, the event won't be sent + // ref https://github.com/getsentry/sentry-javascript/issues/2039 + return null; + }, + dsn: sentryDsn, + integrations: [ + new CaptureConsole({ levels: ["error"] }), + new Dedupe(), + new Offline() + ], + initialScope: { + tags: { - "process": processName - } - }, - environment: isProduction ? "production" : "development", - }); - - if (sentryDsnIsValid) { - logger.info(`✔️ [SENTRY-INIT]: Sentry for ${processName} is initialized.`); - } else { - logger.info(`⚠️ [SENTRY-INIT]: Sentry for ${processName} is initialized but cant transport exceptions because dsn is not valid`); - } - - } catch (error) { - logger.warn(`⚠️ [SENTRY-INIT]: Sentry.init() error: ${error?.message ?? error}.`); - } + "process": processName + } + }, + environment: isProduction ? "production" : "development", + }); } diff --git a/src/common/utils/index.ts b/src/common/utils/index.ts index c10149fb38..1644c07f36 100644 --- a/src/common/utils/index.ts +++ b/src/common/utils/index.ts @@ -51,7 +51,6 @@ export * from "./tar"; export * from "./toggle-set"; export * from "./toJS"; export * from "./type-narrowing"; -export * from "./isValidSentryDsn"; import * as iter from "./iter"; diff --git a/src/common/utils/isValidSentryDsn.ts b/src/common/utils/isValidSentryDsn.ts deleted file mode 100644 index 10033e5013..0000000000 --- a/src/common/utils/isValidSentryDsn.ts +++ /dev/null @@ -1,38 +0,0 @@ -/** - * Copyright (c) 2021 OpenLens Authors - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of - * this software and associated documentation files (the "Software"), to deal in - * the Software without restriction, including without limitation the rights to - * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of - * the Software, and to permit persons to whom the Software is furnished to do so, - * subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS - * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR - * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER - * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN - * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - - -// Regular expression used to parse a Sentry Dsn. -// from https://github.com/getsentry/sentry-javascript/blob/f624f442fd76bf655de6def9fa4d7631735ebba0/packages/utils/src/dsn.ts#L6 -const DSN_REGEX = /^(?:(\w+):)\/\/(?:(\w+)(?::(\w+))?@)([\w.-]+)(?::(\d+))?\/(.+)/; - -/** - * check if string is a valid Sentry dsn - * - * @param {(string | undefined | null)} string - * @return {boolean} - */ -export const isValidSentryDsn = (string: string | undefined | null) => { - - const valid = DSN_REGEX.exec(string); - - return valid; -}; diff --git a/src/common/vars.ts b/src/common/vars.ts index dd9a89ca79..0dea82cab6 100644 --- a/src/common/vars.ts +++ b/src/common/vars.ts @@ -24,7 +24,6 @@ import path from "path"; import { SemVer } from "semver"; import packageInfo from "../../package.json"; import { defineGlobal } from "./utils/defineGlobal"; -import { isValidSentryDsn } from "./utils/isValidSentryDsn"; export const isMac = process.platform === "darwin"; export const isWindows = process.platform === "win32"; @@ -73,4 +72,3 @@ export const appSemVer = new SemVer(packageInfo.version); export const docsUrl = "https://docs.k8slens.dev/main/" as string; export const sentryDsn = packageInfo.config?.sentryDsn ?? ""; -export const sentryDsnIsValid = isValidSentryDsn(sentryDsn); diff --git a/src/renderer/components/+preferences/preferences.tsx b/src/renderer/components/+preferences/preferences.tsx index 3ec4693fe8..6e9ae5f210 100644 --- a/src/renderer/components/+preferences/preferences.tsx +++ b/src/renderer/components/+preferences/preferences.tsx @@ -41,7 +41,7 @@ import { FormSwitch, Switcher } from "../switch"; import { KubeconfigSyncs } from "./kubeconfig-syncs"; import { SettingLayout } from "../layout/setting-layout"; import { Checkbox } from "../checkbox"; -import { sentryDsnIsValid } from "../../../common/vars"; +import { sentryDsn } from "../../../common/vars"; enum Pages { Application = "application", @@ -259,7 +259,7 @@ export class Preferences extends React.Component {

Telemetry

{telemetryExtensions.map(this.renderExtension)} - {sentryDsnIsValid ? ( + {sentryDsn ? (