middleware
middleware copied to clipboard
sentry middleware handler issue
TypeError: Cannot read properties of undefined (reading 'setContext')
app.use('*', async (c: Context, next) => {
sentry({
dsn: 'example.com',
tracesSampleRate: parseFloat(
(c.env.SENTRY_TRACES_SAMPLE_RATE as string) || '0',
),
environment:
c.env.SENTRY_ENV === 'master' ? 'production' : c.env.SENTRY_ENV,
release:
`${packageJson.name}@${packageJson.version}` +
(c.env.SENTRY_ENV === 'master' ? '' : '-' + c.env.SENTRY_ENV),
});
await next();
`});`
sentry does not get initialised, but if I do this directly, it works:
app.use('*', sentry({
dsn: 'example.com',
});
But I need it in the handler so I can add variables from env context
@sam-lippert can you check this out? @yusukebe
@osameyy You shouldn't need to call await next()
since this happens from within the sentry(...)
middleware call, but you should be able to pass the c
and next
arguments through to the middleware to initialize it using c.env
:
app.use('*', async (c: Context, next) => {
return sentry({
dsn: 'example.com',
tracesSampleRate: parseFloat(
(c.env.SENTRY_TRACES_SAMPLE_RATE as string) || '1.0',
),
environment:
c.env.SENTRY_ENV === 'master' ? 'production' : c.env.SENTRY_ENV,
release:
`${packageJson.name}@${packageJson.version}` +
(c.env.SENTRY_ENV === 'master' ? '' : '-' + c.env.SENTRY_ENV),
})(c, next);
});