2023-08-24 10:21:16 +08:00
# Sentry Middleware for Hono
2022-08-04 07:54:30 +08:00
2025-03-19 16:53:11 +08:00
[](https://codecov.io/github/honojs/middleware)
2023-08-24 10:21:16 +08:00
This middleware integrates [Hono ](https://github.com/honojs/hono ) with Sentry. It captures exceptions and sends them to the specified Sentry data source name (DSN) using [toucan-js ](https://github.com/robertcepa/toucan-js ).
2022-08-04 07:54:30 +08:00
2023-08-24 10:21:16 +08:00
## Installation
```plain
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` :
```plain
SENTRY_DSN=< Your DSN >
```
On other platforms, you can directly provide the DSN by passing it as an option:
```ts
sentry({
dsn: `<Your DSN>` ,
})
```
## How to Use
2022-08-04 07:54:30 +08:00
```ts
import { Hono } from 'hono'
2023-02-04 15:19:47 +08:00
import { sentry } from '@hono/sentry'
2022-08-04 07:54:30 +08:00
const app = new Hono()
2022-08-10 00:08:36 +08:00
app.use('*', sentry())
2022-08-04 07:54:30 +08:00
app.get('/', (c) => c.text('foo'))
export default app
```
2023-08-24 10:21:16 +08:00
Options:
```ts
import type { Options as ToucanOptions } from 'toucan-js'
type Options = Omit< ToucanOptions , ' request ' | ' context ' >
```
### For Deno Users
2022-08-04 07:54:30 +08:00
```ts
import { serve } from 'https://deno.land/std/http/server.ts'
2023-02-04 15:08:30 +08:00
import { sentry } from 'npm:@hono/sentry'
2022-08-04 07:54:30 +08:00
import { Hono } from 'https://deno.land/x/hono/mod.ts'
const app = new Hono()
2022-08-12 11:41:03 +08:00
app.use('*', sentry({ dsn: 'https://xxxxxx@xxx.ingest.sentry.io/xxxxxx' }))
2022-08-04 07:54:30 +08:00
app.get('/', (c) => c.text('foo'))
serve(app.fetch)
```
2023-08-24 10:21:16 +08:00
### Accessing an instance of `Sentry`
You can retrieve an instance of `Sentry` using `c.get('sentry')` .
```ts
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
2022-08-04 07:54:30 +08:00
2023-08-24 10:21:16 +08:00
- Samuel Lippert - < https: // github . com / sam-lippert >
- Yusuke Wada - < https: // github . com / yusukebe >
2022-08-04 07:54:30 +08:00
## License
MIT