diff --git a/src/main.ts b/src/main.ts index 5204b698..938d51d2 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,3 +1,7 @@ +// Import this first from sentry instrument! +import '@utils/instrumentSentry'; + +// Now import other modules import { ProviderFiles } from '@api/provider/sessions'; import { PrismaRepository } from '@api/repository/repository.service'; import { HttpStatus, router } from '@api/routes/index.router'; @@ -21,19 +25,6 @@ function initWA() { async function bootstrap() { const logger = new Logger('SERVER'); const app = express(); - const dsn = process.env.SENTRY_DSN; - - if (dsn) { - logger.info('Sentry - ON'); - Sentry.init({ - dsn: dsn, - environment: process.env.NODE_ENV || 'development', - tracesSampleRate: 1.0, - profilesSampleRate: 1.0, - }); - - Sentry.setupExpressErrorHandler(app); - } let providerFiles: ProviderFiles = null; if (configService.get('PROVIDER').ENABLED) { @@ -141,6 +132,14 @@ async function bootstrap() { eventManager.init(server); + if (process.env.SENTRY_DSN) { + logger.info('Sentry - ON'); + + // Add this after all routes, + // but before any and other error-handling middlewares are defined + Sentry.setupExpressErrorHandler(app); + } + server.listen(httpServer.PORT, () => logger.log(httpServer.TYPE.toUpperCase() + ' - ON: ' + httpServer.PORT)); initWA(); diff --git a/src/utils/instrumentSentry.ts b/src/utils/instrumentSentry.ts new file mode 100644 index 00000000..8a660675 --- /dev/null +++ b/src/utils/instrumentSentry.ts @@ -0,0 +1,12 @@ +import * as Sentry from "@sentry/node"; + +const dsn = process.env.SENTRY_DSN; + +if (dsn) { + Sentry.init({ + dsn: dsn, + environment: process.env.NODE_ENV || 'development', + tracesSampleRate: 1.0, + profilesSampleRate: 1.0, + }); +} \ No newline at end of file