From 31745b38daef1773018d004397f31096de9f8470 Mon Sep 17 00:00:00 2001 From: "Hung-Han (Henry) Chen" Date: Fri, 9 Jul 2021 14:21:00 +0300 Subject: [PATCH] Remove sentryIsInitialized (not in used), remove requireSentry hack as we already enable node integration in renderer process, this https://docs.sentry.io/platforms/javascript/guides/electron/#webpack-configuration mod.require shpuld be avaible in renderer; add try..catch block around serStore.getInstance()?.allowErrorReporting to set allowErrorReporting default to false. Make SentryInit() sync Signed-off-by: Hung-Han (Henry) Chen --- src/common/sentry.ts | 95 +++++++++++++++++++++----------------------- 1 file changed, 46 insertions(+), 49 deletions(-) diff --git a/src/common/sentry.ts b/src/common/sentry.ts index 36435b86c9..653cc30bdb 100644 --- a/src/common/sentry.ts +++ b/src/common/sentry.ts @@ -20,73 +20,70 @@ */ import { CaptureConsole, Dedupe, Offline } from "@sentry/integrations"; +import * as Sentry from "@sentry/electron"; import { sentryDsn, isProduction } from "./vars"; import { UserStore } from "./user-store"; import logger from "../main/logger"; -export let sentryIsInitialized = false; - /** - * This function bypasses webpack issues. - * - * See: https://docs.sentry.io/platforms/javascript/guides/electron/#webpack-configuration + * "Translate" 'browser' to 'main' as Lens developer more familiar with the term 'main' */ -async function requireSentry() { - switch (process.type) { - case "browser": - return import("@sentry/electron/dist/main"); - case "renderer": - return import("@sentry/electron/dist/renderer"); - default: - throw new Error(`Unsupported process type ${process.type}`); +function mapProcessName(processType: string) { + if (processType === "browser") { + return "main"; } + + return processType; } /** * Initialize Sentry for the current process so to send errors for debugging. */ -export async function SentryInit(): Promise { +export function SentryInit() { try { - const Sentry = await requireSentry(); + const processName = mapProcessName(process.type); - try { - Sentry.init({ - beforeSend: event => { - if (UserStore.getInstance().allowErrorReporting) { - return event; - } + Sentry.init({ + beforeSend: (event) => { + // default to false, in case instance of UserStore is not created (yet) + let allowErrorReporting = false; - logger.info(`🔒 [SENTRY-BEFORE-SEND-HOOK]: allowErrorReporting: false. 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 ==="); + try { + allowErrorReporting = UserStore.getInstance()?.allowErrorReporting; + } catch { + // ignore the TypeError throw by UserStore.getInstance() + } - // 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: { - // "translate" browser to 'main' as Lens developer more familiar with the term 'main' - "process": process.type === "browser" ? "main" : "renderer" - } - }, - environment: isProduction ? "production" : "development", - }); + if (allowErrorReporting) { + return event; + } - sentryIsInitialized = true; + 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-INIT]: Sentry for ${process.type} is initialized.`); - } catch (error) { - logger.warn(`⚠️ [SENTRY-INIT]: Sentry.init() error: ${error?.message ?? error}.`); - } + // 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", + }); + + logger.info(`✔️ [SENTRY-INIT]: Sentry for ${processName} is initialized.`); } catch (error) { - logger.warn(`⚠️ [SENTRY-INIT]: Error loading Sentry module ${error?.message ?? error}.`); + logger.warn(`⚠️ [SENTRY-INIT]: Sentry.init() error: ${error?.message ?? error}.`); } }