This repository has been archived by the owner on Aug 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add Sentry configuration for better error handling (#2347)
- Loading branch information
1 parent
03f0deb
commit db9818e
Showing
2 changed files
with
18 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,21 @@ | ||
import * as Sentry from '@sentry/node'; | ||
import { CaptureConsole as CaptureConsoleIntegration } from "@sentry/integrations"; | ||
import config from '@config'; | ||
import { ErrorRequestHandler } from 'express'; | ||
import config from '@/config'; | ||
|
||
export const initializeSentry = (app) => { | ||
if (!config.sentryDNS) { | ||
console.log('Sentry DSN not found. Sentry is not initialized.'); | ||
return; | ||
} | ||
|
||
export function initCaptureConsole() { | ||
const logLevel = ['error']; | ||
console.log( | ||
`Initializing Sentry for log level "${logLevel}" and config: ${config.sentryDNS}` | ||
); | ||
Sentry.init({ | ||
dsn: config.sentryDNS as string, | ||
// https://docs.sentry.io/platforms/javascript/configuration/integrations/plugin/#captureconsole | ||
integrations: [new CaptureConsoleIntegration({ levels: logLevel })], | ||
dsn: process.env.SENTRY_DSN, | ||
tracesSampleRate: 1.0, | ||
}); | ||
} | ||
|
||
export function initCaptureConsoleWithHandler(app) { | ||
if (config.sentryDNS) { | ||
initCaptureConsole(); | ||
app.use(Sentry.Handlers.requestHandler()); | ||
app.use(Sentry.Handlers.tracingHandler()); | ||
}; | ||
|
||
// RequestHandler creates a separate execution context using domains, so that every | ||
// transaction/span/breadcrumb is attached to its own Hub instance | ||
app.use(Sentry.Handlers.requestHandler()); | ||
|
||
// The error handler must be before any other error middleware and after all controllers | ||
app.use(Sentry.Handlers.errorHandler()); | ||
} else { | ||
console.log( | ||
'Sentry was not initialized as SENTRY_DNS env variable is missing' | ||
); | ||
} | ||
} | ||
export const sentryErrorHandler: ErrorRequestHandler = | ||
Sentry.Handlers.errorHandler(); |