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 * 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<void> {
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}.`);
}
}