honojs-middleware/packages/sentry
Jonathan Haines f0475c7324
refactor(sentry): enable isolated declarations (#1205)
2025-06-09 18:31:52 +09:00
..
src refactor(sentry): enable isolated declarations (#1205) 2025-06-09 18:31:52 +09:00
CHANGELOG.md Version Packages (#1082) 2025-03-29 09:20:18 +09:00
README.md chore: add coverage badges (#1023) 2025-03-19 17:53:11 +09:00
package.json build: typescript project references (#1077) 2025-04-02 18:28:02 +09:00
tsconfig.build.json refactor(sentry): enable isolated declarations (#1205) 2025-06-09 18:31:52 +09:00
tsconfig.json build: typescript project references (#1077) 2025-04-02 18:28:02 +09:00
tsconfig.spec.json feat(eslint-config): enable linting with type information (#1098) 2025-04-07 19:31:09 +09:00
vitest.config.ts test(workspace): upgrade to vitest v3 (#1009) 2025-03-12 12:52:15 +09:00

README.md

Sentry Middleware for Hono

codecov

This middleware integrates Hono with Sentry. It captures exceptions and sends them to the specified Sentry data source name (DSN) using toucan-js.

Installation

npm i hono @hono/sentry

Configuration

If you're running your application on Cloudflare Workers, set a binding value named SENTRY_DSN, which will be used as the DSN. For instance, during development, you can specify this in .dev.vars:

SENTRY_DSN=<Your DSN>

On other platforms, you can directly provide the DSN by passing it as an option:

sentry({
  dsn: `<Your DSN>`,
})

How to Use

import { Hono } from 'hono'
import { sentry } from '@hono/sentry'

const app = new Hono()

app.use('*', sentry())
app.get('/', (c) => c.text('foo'))

export default app

Options:

import type { Options as ToucanOptions } from 'toucan-js'
type Options = Omit<ToucanOptions, 'request' | 'context'>

For Deno Users

import { serve } from 'https://deno.land/std/http/server.ts'
import { sentry } from 'npm:@hono/sentry'
import { Hono } from 'https://deno.land/x/hono/mod.ts'

const app = new Hono()

app.use('*', sentry({ dsn: 'https://xxxxxx@xxx.ingest.sentry.io/xxxxxx' }))
app.get('/', (c) => c.text('foo'))

serve(app.fetch)

Accessing an instance of Sentry

You can retrieve an instance of Sentry using c.get('sentry').

app.onError((e, c) => {
  c.get('sentry').setContext('character', {
    name: 'Mighty Fighter',
    age: 19,
    attack_type: 'melee',
  })
  c.get('sentry').captureException(e)
  return c.text('Internal Server Error', 500)
})

Authors

License

MIT