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

move most variables to init params, remove initLogError

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2021-07-08 16:04:34 -04:00
parent dd5cd89ba5
commit e643067d0e
2 changed files with 37 additions and 58 deletions

View File

@ -20,60 +20,19 @@
*/ */
import { CaptureConsole, Dedupe, Offline } from "@sentry/integrations"; import { CaptureConsole, Dedupe, Offline } from "@sentry/integrations";
import type { Event as SentryEvent } from "@sentry/types";
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";
// https://www.electronjs.org/docs/api/process#processtype-readonly async function requireSentry() {
type ElectronProcessType = "browser" | "renderer" | "worker"; switch (process.type) {
case "browser":
const processType = process.type as ElectronProcessType; return import("@sentry/electron/dist/main");
case "renderer":
export const integrations = [ return import("@sentry/electron/dist/renderer");
new CaptureConsole({ levels: ["error"] }), default:
new Dedupe(), throw new Error(`Unsupported process type ${process.type}`);
new Offline()
];
const initialScope = {
tags: {
// "translate" browser to 'main' as Lens developer more familiar with the term 'main'
"process": processType === "browser" ? "main" : "renderer"
} }
};
const environment = isProduction ? "production" : "development";
function logInitError(reason: string) {
logger.warn(`⚠️ [SENTRY-INIT]: ${reason}, Sentry is not initialized.`);
}
function beforeSend(event: SentryEvent) {
if (UserStore.getInstance().allowErrorReporting) {
return event;
}
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 ===");
// if return null, the event won't be sent
// ref https://github.com/getsentry/sentry-javascript/issues/2039
return null;
}
async function requireSentry() {
if (processType === "browser") {
return import("@sentry/electron/dist/main");
}
if (processType === "renderer") {
return import("@sentry/electron/dist/renderer");
}
throw new Error(`Unsupported process type ${processType}`);
} }
export async function SentryInit() { export async function SentryInit() {
@ -82,17 +41,39 @@ export async function SentryInit() {
try { try {
Sentry.init({ Sentry.init({
beforeSend, beforeSend: event => {
if (UserStore.getInstance().allowErrorReporting) {
return event;
}
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 ===");
// if return null, the event won't be sent
// ref https://github.com/getsentry/sentry-javascript/issues/2039
return null;
},
dsn: sentryDsn, dsn: sentryDsn,
integrations, integrations: [
initialScope, new CaptureConsole({ levels: ["error"] }),
environment 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",
}); });
logger.info(`✔️ [SENTRY-INIT]: Sentry for ${processType} is initialized.`); logger.info(`✔️ [SENTRY-INIT]: Sentry for ${process.type} is initialized.`);
} catch (error) { } catch (error) {
return logInitError(`Sentry.init() error ${error?.message}`); logger.warn(`⚠️ [SENTRY-INIT]: Sentry.init() error: ${error?.message ?? error}.`);
} }
} catch (error) { } catch (error) {
return logInitError(`Error loading Sentry module ${error?.message ?? error}`); logger.warn(`⚠️ [SENTRY-INIT]: Error loading Sentry module ${error?.message ?? error}.`);
} }
} }

View File

@ -51,8 +51,6 @@ export * from "./tar";
export * from "./toggle-set"; export * from "./toggle-set";
export * from "./toJS"; export * from "./toJS";
export * from "./type-narrowing"; export * from "./type-narrowing";
export * from "./isValidURL";
export * from "./isValidSentryDsn";
import * as iter from "./iter"; import * as iter from "./iter";