From ba362dcbe84da92ffc2b26c9f36d796df6259721 Mon Sep 17 00:00:00 2001 From: Joakim Uddholm Date: Mon, 18 Nov 2024 16:10:49 +0100 Subject: [PATCH] fix: sentry should not load if sentryDSN is not set --- api/src/app.ts | 7 +++++-- api/src/util/sentry.ts | 7 +++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/api/src/app.ts b/api/src/app.ts index 867a1c2e..261460f5 100644 --- a/api/src/app.ts +++ b/api/src/app.ts @@ -50,7 +50,7 @@ export async function createApp(dal: DataAccessLayer) { const app = express(); // Sentry has to be set up before everything else. - initSentry(app); + initSentry(); // Metrics middleware app.use(metricsMiddleware); @@ -273,7 +273,10 @@ export async function createApp(dal: DataAccessLayer) { } // Sentry Error Handler - Sentry.setupExpressErrorHandler(app); + if (config.sentryDSN) { + Sentry.setupExpressErrorHandler(app); + } + // Global Error Handler. Should catch anything that propagates up from the REST routes. app.use(errorHandler); diff --git a/api/src/util/sentry.ts b/api/src/util/sentry.ts index ea0eef31..92831f2a 100644 --- a/api/src/util/sentry.ts +++ b/api/src/util/sentry.ts @@ -1,6 +1,6 @@ import { config } from "@gram/core/dist/config/index.js"; import * as Sentry from "@sentry/node"; -import { nodeProfilingIntegration } from "@sentry/profiling-node"; + import { Express } from "express"; import log4js from "log4js"; import { version } from "./version.js"; @@ -24,12 +24,15 @@ export function hasSentry() { return !!config.sentryDSN; } -export function initSentry(app: Express) { +export function initSentry() { const sentryDSN = config.sentryDSN; if (!sentryDSN) { return; } + // eslint-disable-next-line @typescript-eslint/no-var-requires + const { nodeProfilingIntegration } = require("@sentry/profiling-node"); + Sentry.init({ release: `gram@${version}`, environment: process.env["NODE_ENV"],