chore: updates types and readme

This commit is contained in:
Jessica Boezwinkle
2023-06-23 16:02:01 +01:00
parent 9182e79c2d
commit f615b8cdf2
3 changed files with 53 additions and 46 deletions

View File

@@ -48,6 +48,10 @@ export default config;
### Additional Options ### Additional Options
- `enabled`: boolean | optional
Set to false to disable the plugin. Defaults to true.
- `init` : ClientOptions | optional - `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). 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).

View File

@@ -9,51 +9,54 @@ import type { PluginOptions } from './types'
export const startSentry = (pluginOptions: PluginOptions): void => { export const startSentry = (pluginOptions: PluginOptions): void => {
const { dsn, options } = pluginOptions const { dsn, options } = pluginOptions
try { if (dsn)
const app = express() try {
const app = express()
Sentry.init({ Sentry.init({
...options?.init, ...options?.init,
dsn: dsn, dsn: dsn,
integrations: [ integrations: [
...(options?.init?.integrations || []), ...(options?.init?.integrations || []),
new Sentry.Integrations.Http({ tracing: true }), new Sentry.Integrations.Http({ tracing: true }),
new Sentry.Integrations.Express({ app }), new Sentry.Integrations.Express({ app }),
...Sentry.autoDiscoverNodePerformanceMonitoringIntegrations(), ...Sentry.autoDiscoverNodePerformanceMonitoringIntegrations(),
], ],
}) })
app.use(Sentry.Handlers.requestHandler(options?.requestHandler || {}) as express.RequestHandler) app.use(
app.use(Sentry.Handlers.tracingHandler()) Sentry.Handlers.requestHandler(options?.requestHandler || {}) as express.RequestHandler,
)
app.use(Sentry.Handlers.tracingHandler())
app.use( app.use(
Sentry.Handlers.errorHandler({ Sentry.Handlers.errorHandler({
shouldHandleError(error) { shouldHandleError(error) {
if (error.status === 500) { if (error.status === 500) {
return true return true
} }
if ( if (
options?.captureErrors && options?.captureErrors &&
typeof error.status === 'number' && typeof error.status === 'number' &&
options.captureErrors.includes(error.status) options.captureErrors.includes(error.status)
) { ) {
return true return true
} }
return false return false
}, },
}) as express.ErrorRequestHandler, }) as express.ErrorRequestHandler,
) )
app.use(function onError( app.use(function onError(
_err: unknown, _err: unknown,
_req: Request, _req: Request,
res: Response & { sentry?: string }, res: Response & { sentry?: string },
_next: NextFunction, _next: NextFunction,
) { ) {
res.statusCode = 500 res.statusCode = 500
res.end(res.sentry + '\n') res.end(res.sentry + '\n')
}) })
} catch (err: unknown) { } catch (err: unknown) {
console.log('There was an error initializing Sentry, please ensure you entered a valid DSN') console.log('There was an error initializing Sentry, please ensure you entered a valid DSN')
} }
} }

View File

@@ -2,7 +2,7 @@ import type { RequestHandlerOptions } from '@sentry/node/types/handlers'
import type { ClientOptions } from '@sentry/types' import type { ClientOptions } from '@sentry/types'
export interface PluginOptions { export interface PluginOptions {
dsn?: string dsn: string | null
enabled?: boolean enabled?: boolean
options?: { options?: {
init?: Partial<ClientOptions> init?: Partial<ClientOptions>