1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/src/common/sentry.ts
Sebastian Malton 797c6487bf Bump all @sentry related packages
Signed-off-by: Sebastian Malton <sebastian@malton.name>
2022-03-31 14:21:48 -04:00

66 lines
2.0 KiB
TypeScript

/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { Dedupe, Offline } from "@sentry/integrations";
import { sentryDsn, isProduction } from "./vars";
import { UserStore } from "./user-store";
import { inspect } from "util";
/**
* "Translate" 'browser' to 'main' as Lens developer more familiar with the term 'main'
*/
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() {
const processName = mapProcessName(process.type);
const { init } = process.type === "browser"
? await import("@sentry/electron/main")
: await import("@sentry/electron/renderer");
init({
beforeSend: (event) => {
// default to false, in case instance of UserStore is not created (yet)
const allowErrorReporting = UserStore.getInstance(false)?.allowErrorReporting ?? false;
if (allowErrorReporting) {
return event;
}
/**
* Directly write to stdout so that no other integrations capture this and create an infinite loop
*/
process.stdout.write(`🔒 [SENTRY-BEFORE-SEND-HOOK]: allowErrorReporting: ${allowErrorReporting}. Sentry event is caught but not sent to server.`);
process.stdout.write("🔒 [SENTRY-BEFORE-SEND-HOOK]: === START OF SENTRY EVENT ===");
process.stdout.write(inspect(event, false, null, true));
process.stdout.write("🔒 [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 Dedupe(),
new Offline(),
],
initialScope: {
tags: {
"process": processName,
},
},
environment: isProduction ? "production" : "development",
});
}