middleware icon indicating copy to clipboard operation
middleware copied to clipboard

sentry middleware handler issue

Open osameyy opened this issue 10 months ago • 2 comments

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

osameyy avatar Apr 25 '24 10:04 osameyy

@sam-lippert can you check this out? @yusukebe

osameyy avatar Apr 28 '24 08:04 osameyy

@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);
});

sam-lippert avatar Jul 08 '24 03:07 sam-lippert