diff --git a/packages/plugin-sentry/README.md b/packages/plugin-sentry/README.md index 6cdb4b7ca..d508764d0 100644 --- a/packages/plugin-sentry/README.md +++ b/packages/plugin-sentry/README.md @@ -16,9 +16,9 @@ This plugin seamlessly integrates [Sentry](https://sentry.io/) with [Payload](ht 1. Import `sentry` from `'@payloadcms/plugin-sentry'` 2. Add it to the `plugins` array of your [Payload config](https://payloadcms.com/docs/configuration/overview) -3. Pass in your Data Source Name (DSN) +3. Pass in your Data Source Name (DSN) 4. Pass [additional options](#additional-options) - *not required* - + ```js import { buildConfig } from 'payload/config'; import sentry from '@payloadcms/plugin-sentry'; @@ -48,6 +48,10 @@ export default config; ### Additional Options +- `enabled`: boolean | optional + + Set to false to disable the plugin. Defaults to true. + - `init` : ClientOptions | optional Sentry allows a variety of options to be passed into the Sentry.init() function, see the full list of options [here](https://docs.sentry.io/platforms/node/guides/express/configuration/options). diff --git a/packages/plugin-sentry/src/startSentry.ts b/packages/plugin-sentry/src/startSentry.ts index 466435a63..81c227523 100644 --- a/packages/plugin-sentry/src/startSentry.ts +++ b/packages/plugin-sentry/src/startSentry.ts @@ -9,51 +9,54 @@ import type { PluginOptions } from './types' export const startSentry = (pluginOptions: PluginOptions): void => { const { dsn, options } = pluginOptions - try { - const app = express() + if (dsn) + try { + const app = express() - Sentry.init({ - ...options?.init, - dsn: dsn, - integrations: [ - ...(options?.init?.integrations || []), - new Sentry.Integrations.Http({ tracing: true }), - new Sentry.Integrations.Express({ app }), - ...Sentry.autoDiscoverNodePerformanceMonitoringIntegrations(), - ], - }) + Sentry.init({ + ...options?.init, + dsn: dsn, + integrations: [ + ...(options?.init?.integrations || []), + new Sentry.Integrations.Http({ tracing: true }), + new Sentry.Integrations.Express({ app }), + ...Sentry.autoDiscoverNodePerformanceMonitoringIntegrations(), + ], + }) - app.use(Sentry.Handlers.requestHandler(options?.requestHandler || {}) as express.RequestHandler) - app.use(Sentry.Handlers.tracingHandler()) + app.use( + Sentry.Handlers.requestHandler(options?.requestHandler || {}) as express.RequestHandler, + ) + app.use(Sentry.Handlers.tracingHandler()) - app.use( - Sentry.Handlers.errorHandler({ - shouldHandleError(error) { - if (error.status === 500) { - return true - } - if ( - options?.captureErrors && - typeof error.status === 'number' && - options.captureErrors.includes(error.status) - ) { - return true - } - return false - }, - }) as express.ErrorRequestHandler, - ) + app.use( + Sentry.Handlers.errorHandler({ + shouldHandleError(error) { + if (error.status === 500) { + return true + } + if ( + options?.captureErrors && + typeof error.status === 'number' && + options.captureErrors.includes(error.status) + ) { + return true + } + return false + }, + }) as express.ErrorRequestHandler, + ) - app.use(function onError( - _err: unknown, - _req: Request, - res: Response & { sentry?: string }, - _next: NextFunction, - ) { - res.statusCode = 500 - res.end(res.sentry + '\n') - }) - } catch (err: unknown) { - console.log('There was an error initializing Sentry, please ensure you entered a valid DSN') - } + app.use(function onError( + _err: unknown, + _req: Request, + res: Response & { sentry?: string }, + _next: NextFunction, + ) { + res.statusCode = 500 + res.end(res.sentry + '\n') + }) + } catch (err: unknown) { + console.log('There was an error initializing Sentry, please ensure you entered a valid DSN') + } } diff --git a/packages/plugin-sentry/src/types.ts b/packages/plugin-sentry/src/types.ts index 0bd5f98dc..000fa1cbe 100644 --- a/packages/plugin-sentry/src/types.ts +++ b/packages/plugin-sentry/src/types.ts @@ -2,7 +2,7 @@ import type { RequestHandlerOptions } from '@sentry/node/types/handlers' import type { ClientOptions } from '@sentry/types' export interface PluginOptions { - dsn?: string + dsn: string | null enabled?: boolean options?: { init?: Partial