1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

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 <chenhungh@gmail.com>
This commit is contained in:
Hung-Han (Henry) Chen 2021-07-09 14:21:00 +03:00
parent b68288e8e4
commit 31745b38da
No known key found for this signature in database
GPG Key ID: 54B44603D251B788

View File

@ -20,73 +20,70 @@
*/ */
import { CaptureConsole, Dedupe, Offline } from "@sentry/integrations"; import { CaptureConsole, Dedupe, Offline } from "@sentry/integrations";
import * as Sentry from "@sentry/electron";
import { sentryDsn, isProduction } from "./vars"; import { sentryDsn, isProduction } from "./vars";
import { UserStore } from "./user-store"; import { UserStore } from "./user-store";
import logger from "../main/logger"; import logger from "../main/logger";
export let sentryIsInitialized = false;
/** /**
* This function bypasses webpack issues. * "Translate" 'browser' to 'main' as Lens developer more familiar with the term 'main'
*
* See: https://docs.sentry.io/platforms/javascript/guides/electron/#webpack-configuration
*/ */
async function requireSentry() { function mapProcessName(processType: string) {
switch (process.type) { if (processType === "browser") {
case "browser": return "main";
return import("@sentry/electron/dist/main");
case "renderer":
return import("@sentry/electron/dist/renderer");
default:
throw new Error(`Unsupported process type ${process.type}`);
} }
return processType;
} }
/** /**
* Initialize Sentry for the current process so to send errors for debugging. * Initialize Sentry for the current process so to send errors for debugging.
*/ */
export async function SentryInit(): Promise<void> { export function SentryInit() {
try { try {
const Sentry = await requireSentry(); const processName = mapProcessName(process.type);
try { Sentry.init({
Sentry.init({ beforeSend: (event) => {
beforeSend: event => { // default to false, in case instance of UserStore is not created (yet)
if (UserStore.getInstance().allowErrorReporting) { let allowErrorReporting = false;
return event;
}
logger.info(`🔒 [SENTRY-BEFORE-SEND-HOOK]: allowErrorReporting: false. Sentry event is caught but not sent to server.`); try {
logger.info("🔒 [SENTRY-BEFORE-SEND-HOOK]: === START OF SENTRY EVENT ==="); allowErrorReporting = UserStore.getInstance()?.allowErrorReporting;
logger.info(event); } catch {
logger.info("🔒 [SENTRY-BEFORE-SEND-HOOK]: === END OF SENTRY EVENT ==="); // ignore the TypeError throw by UserStore.getInstance()
}
// if return null, the event won't be sent if (allowErrorReporting) {
// ref https://github.com/getsentry/sentry-javascript/issues/2039 return event;
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",
});
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.`); // if return null, the event won't be sent
} catch (error) { // ref https://github.com/getsentry/sentry-javascript/issues/2039
logger.warn(`⚠️ [SENTRY-INIT]: Sentry.init() error: ${error?.message ?? error}.`); 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) { } catch (error) {
logger.warn(`⚠️ [SENTRY-INIT]: Error loading Sentry module ${error?.message ?? error}.`); logger.warn(`⚠️ [SENTRY-INIT]: Sentry.init() error: ${error?.message ?? error}.`);
} }
} }