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

Refactor into SentryInit(), add try...catch block for Sentry.init and others

Signed-off-by: Hung-Han (Henry) Chen <chenhungh@gmail.com>
This commit is contained in:
Hung-Han (Henry) Chen 2021-07-08 10:15:45 +03:00
parent 9c186ab65b
commit 3e43757b01
No known key found for this signature in database
GPG Key ID: 54B44603D251B788
3 changed files with 110 additions and 55 deletions

105
src/common/sentry.ts Normal file
View File

@ -0,0 +1,105 @@
/**
* Copyright (c) 2021 OpenLens Authors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import { CaptureConsole, Dedupe, Offline } from "@sentry/integrations";
import { sentryDsn, sentryDsnIsValid, isProduction } from "./vars";
import { UserStore } from "./user-store";
import logger from "../main/logger";
// https://www.electronjs.org/docs/api/process#processtype-readonly
const electronProcessType = ["browser", "renderer", "worker"] as const;
const integrations = [
new CaptureConsole({ levels: ["error"] }),
new Dedupe(),
new Offline()
];
const initialScope = (processType: typeof electronProcessType[number]) => {
return {
tags: {
// "translate" browser to 'main' as Lens developer more familiar with the term 'main'
"process": processType === "browser" ? "main" : "renderer"
}
};
};
const environment = isProduction ? "production" : "development";
const logInitError = (reason: string) => {
logger.error(`⚠️ [SENTRY-INIT]: ${reason}, Sentry is not initialized.`);
};
export const SentryInit = () => {
if (!sentryDsnIsValid) {
logInitError("Sentry DSN is not valid");
return;
}
const processType = process.type as typeof electronProcessType[number];
let Sentry;
try {
// if in main process
if (processType === "browser") {
Sentry = require("@sentry/electron/dist/main");
}
// if in renderer process
if (processType === "renderer") {
Sentry = require("@sentry/electron/dist/renderer");
}
} catch (error) {
logInitError(`Error loading Sentry module ${error?.message}`);
return;
}
if (!Sentry) {
logInitError(`Unsupported process type ${processType}`);
return;
}
try {
Sentry.init({
beforeSend(event: unknown) {
const allowErrorReporting = UserStore.getInstance().allowErrorReporting;
if (allowErrorReporting) return event;
logger.info(`🔒 [SENTRY-BEFORE-SEND-HOOK]: allowErrorReporting: ${allowErrorReporting}. Sentry event is caught but not sent to server.`);
logger.info(event);
return null;
},
dsn: sentryDsn,
integrations,
initialScope: initialScope(processType),
environment
});
} catch (error) {
logInitError(`Sentry.init() error ${error?.message}`);
return;
}
};

View File

@ -26,7 +26,7 @@ import * as Mobx from "mobx";
import * as LensExtensionsCommonApi from "../extensions/common-api"; import * as LensExtensionsCommonApi from "../extensions/common-api";
import * as LensExtensionsMainApi from "../extensions/main-api"; import * as LensExtensionsMainApi from "../extensions/main-api";
import { app, autoUpdater, dialog, powerMonitor } from "electron"; import { app, autoUpdater, dialog, powerMonitor } from "electron";
import { appName, isMac, productName, sentryDsn, sentryDsnIsValid, isProduction } from "../common/vars"; import { appName, isMac, productName } from "../common/vars";
import path from "path"; import path from "path";
import { LensProxy } from "./proxy/lens-proxy"; import { LensProxy } from "./proxy/lens-proxy";
import { WindowManager } from "./window-manager"; import { WindowManager } from "./window-manager";
@ -59,33 +59,9 @@ import { UserStore } from "../common/user-store";
import { WeblinkStore } from "../common/weblink-store"; import { WeblinkStore } from "../common/weblink-store";
import { ExtensionsStore } from "../extensions/extensions-store"; import { ExtensionsStore } from "../extensions/extensions-store";
import { FilesystemProvisionerStore } from "./extension-filesystem"; import { FilesystemProvisionerStore } from "./extension-filesystem";
import { CaptureConsole, Dedupe, Offline } from "@sentry/integrations"; import { SentryInit } from "../common/sentry";
import * as Sentry from "@sentry/electron/dist/main";
if (sentryDsnIsValid) {
Sentry.init({
beforeSend(event) {
const allow = UserStore.getInstance().allowErrorReporting;
if (allow) return event;
return null;
},
dsn: sentryDsn,
integrations: [
new CaptureConsole({ levels: ["error"] }),
new Dedupe(),
new Offline()
],
initialScope: {
tags: {
"process": "main"
}
},
environment: isProduction ? "production" : "development"
});
}
SentryInit();
const workingDir = path.join(app.getPath("appData"), appName); const workingDir = path.join(app.getPath("appData"), appName);
const cleanup = disposer(); const cleanup = disposer();

View File

@ -36,35 +36,9 @@ import { registerIpcHandlers } from "./ipc";
import { ipcRenderer } from "electron"; import { ipcRenderer } from "electron";
import { IpcRendererNavigationEvents } from "./navigation/events"; import { IpcRendererNavigationEvents } from "./navigation/events";
import { catalogEntityRegistry } from "./api/catalog-entity-registry"; import { catalogEntityRegistry } from "./api/catalog-entity-registry";
import { sentryDsn, sentryDsnIsValid, isProduction } from "../common/vars"; import { SentryInit } from "../common/sentry";
import { CaptureConsole, Dedupe, Offline } from "@sentry/integrations";
import * as Sentry from "@sentry/electron/dist/renderer";
import { UserStore } from "../common/user-store";
if (sentryDsnIsValid) {
Sentry.init({
beforeSend(event) {
const allow = UserStore.getInstance().allowErrorReporting;
if (allow) return event;
return null;
},
dsn: sentryDsn,
integrations: [
new CaptureConsole({ levels: ["error"] }),
new Dedupe(),
new Offline()
],
initialScope: {
tags: {
"process": "renderer"
}
},
environment: isProduction ? "production" : "development"
});
}
SentryInit();
@observer @observer
export class LensApp extends React.Component { export class LensApp extends React.Component {