|
10 | 10 | [](https://www.npmjs.com/package/@sentry/hono) |
11 | 11 | [](https://www.npmjs.com/package/@sentry/hono) |
12 | 12 |
|
13 | | -This SDK is compatible with Hono 4+. |
| 13 | +The official Sentry SDK for monitoring Hono applications. |
14 | 14 |
|
15 | | -## Links |
| 15 | +## Documentation |
16 | 16 |
|
17 | | -- [Official SDK Docs](https://docs.sentry.io/platforms/javascript/guides/hono/) |
| 17 | +- [Getting started](https://docs.sentry.io/platforms/javascript/guides/hono/) |
| 18 | +- [Configuration](https://docs.sentry.io/platforms/javascript/guides/hono/configuration/) |
| 19 | +- [Hono-specific features](https://docs.sentry.io/platforms/javascript/guides/hono/features/) |
18 | 20 |
|
19 | | -## Install |
| 21 | +## Support |
20 | 22 |
|
21 | | -To get started, first install the `@sentry/hono` package: |
22 | | - |
23 | | -```bash |
24 | | -npm install @sentry/hono |
25 | | -``` |
26 | | - |
27 | | -## Setup (Cloudflare Workers) |
28 | | - |
29 | | -### 1. Install Peer Dependency |
30 | | - |
31 | | -Additionally to `@sentry/hono`, install the `@sentry/cloudflare` package: |
32 | | - |
33 | | -```bash |
34 | | -npm install --save @sentry/cloudflare |
35 | | -``` |
36 | | - |
37 | | -Make sure the installed version always stays in sync. The `@sentry/cloudflare` package is a required peer dependency when using `@sentry/hono/cloudflare`. |
38 | | -You won't import `@sentry/cloudflare` directly in your code, but it needs to be installed in your project. |
39 | | - |
40 | | -### 2. Enable Node.js compatibility |
41 | | - |
42 | | -Set the `nodejs_compat` compatibility flag in your `wrangler.jsonc`/`wrangler.toml` config. This is because the SDK needs access to the `AsyncLocalStorage` API to work correctly. |
43 | | - |
44 | | -```jsonc {tabTitle:JSON} {filename:wrangler.jsonc} |
45 | | -{ |
46 | | - "compatibility_flags": ["nodejs_compat"], |
47 | | -} |
48 | | -``` |
49 | | - |
50 | | -```toml {tabTitle:Toml} {filename:wrangler.toml} |
51 | | -compatibility_flags = ["nodejs_compat"] |
52 | | -``` |
53 | | - |
54 | | -### 3. Initialize Sentry in your Hono app |
55 | | - |
56 | | -Initialize the Sentry Hono middleware as early as possible in your app: |
57 | | - |
58 | | -```typescript |
59 | | -import { Hono } from 'hono'; |
60 | | -import { sentry } from '@sentry/hono/cloudflare'; |
61 | | - |
62 | | -const app = new Hono(); |
63 | | - |
64 | | -// Initialize Sentry middleware right after creating the app |
65 | | -app.use( |
66 | | - sentry(app, { |
67 | | - dsn: '__DSN__', |
68 | | - // ...other Sentry options |
69 | | - }), |
70 | | -); |
71 | | - |
72 | | -// ... your routes and other middleware |
73 | | - |
74 | | -export default app; |
75 | | -``` |
76 | | - |
77 | | -#### Access `env` from Cloudflare Worker bindings |
78 | | - |
79 | | -Pass the options as a callback instead of a plain options object. The function receives the Cloudflare Worker `env` as defined in the Worker's `Bindings`: |
80 | | - |
81 | | -```typescript |
82 | | -import { Hono } from 'hono'; |
83 | | -import { sentry } from '@sentry/hono/cloudflare'; |
84 | | - |
85 | | -type Bindings = { SENTRY_DSN: string }; |
86 | | - |
87 | | -const app = new Hono<{ Bindings: Bindings }>(); |
88 | | - |
89 | | -app.use(sentry(app, env => ({ dsn: env.SENTRY_DSN }))); |
90 | | - |
91 | | -export default app; |
92 | | -``` |
93 | | - |
94 | | -## Setup (Node) |
95 | | - |
96 | | -### 1. Install Peer Dependency |
97 | | - |
98 | | -Additionally to `@sentry/hono`, install the `@sentry/node` package: |
99 | | - |
100 | | -```bash |
101 | | -npm install --save @sentry/node |
102 | | -``` |
103 | | - |
104 | | -Make sure the installed version always stays in sync. The `@sentry/node` package is a required peer dependency when using `@sentry/hono/node`. |
105 | | -You won't import `@sentry/node` directly in your code, but it needs to be installed in your project. |
106 | | - |
107 | | -### 2. Initialize Sentry in a separate file |
108 | | - |
109 | | -Create an `instrument.mjs` (or `instrument.ts`) file that initializes Sentry before the rest of your application runs. |
110 | | -This ensures Sentry can wrap third-party libraries (e.g. database clients) as early as possible: |
111 | | - |
112 | | -```ts |
113 | | -// instrument.mjs (or instrument.ts) |
114 | | -import * as Sentry from '@sentry/hono/node'; |
115 | | - |
116 | | -Sentry.init({ |
117 | | - dsn: '__DSN__', |
118 | | - tracesSampleRate: 1.0, |
119 | | -}); |
120 | | -``` |
121 | | - |
122 | | -### 3. Load the instrument file with `--import` |
123 | | - |
124 | | -When starting your Hono Node application, use the `--import` CLI flag to load `instrument.mjs` before your app code: |
125 | | - |
126 | | -```bash |
127 | | -node --import ./instrument.mjs app.js |
128 | | -``` |
129 | | - |
130 | | -This option can also be added to the `NODE_OPTIONS` environment variable: |
131 | | - |
132 | | -```bash |
133 | | -NODE_OPTIONS="--import ./instrument.mjs" |
134 | | -``` |
135 | | - |
136 | | -### 4. Add the Sentry middleware to your Hono app |
137 | | - |
138 | | -Add the `sentry` middleware to your Hono app. Since Sentry was already initialized in the instrument file, no options are passed here: |
139 | | - |
140 | | -```ts |
141 | | -import { Hono } from 'hono'; |
142 | | -import { serve } from '@hono/node-server'; |
143 | | -import { sentry } from '@sentry/hono/node'; |
144 | | - |
145 | | -const app = new Hono(); |
146 | | - |
147 | | -// Add Sentry middleware right after creating the app |
148 | | -app.use(sentry(app)); |
149 | | - |
150 | | -// ... your routes and other middleware |
151 | | - |
152 | | -serve(app); |
153 | | -``` |
154 | | - |
155 | | -## Setup (Bun) |
156 | | - |
157 | | -### 1. Install Peer Dependency |
158 | | - |
159 | | -Additionally to `@sentry/hono`, install the `@sentry/bun` package: |
160 | | - |
161 | | -```bash |
162 | | -npm install --save @sentry/bun |
163 | | -``` |
164 | | - |
165 | | -Make sure the installed version always stays in sync. The `@sentry/bun` package is a required peer dependency when using `@sentry/hono/bun`. |
166 | | -You won't import `@sentry/bun` directly in your code, but it needs to be installed in your project. |
167 | | - |
168 | | -### 2. Initialize Sentry in your Hono app |
169 | | - |
170 | | -Initialize the Sentry Hono middleware as early as possible in your app: |
171 | | - |
172 | | -```ts |
173 | | -import { Hono } from 'hono'; |
174 | | -import { serve } from '@hono/node-server'; |
175 | | -import { sentry } from '@sentry/hono/bun'; |
176 | | - |
177 | | -const app = new Hono(); |
178 | | - |
179 | | -// Initialize Sentry middleware right after creating the app |
180 | | -app.use( |
181 | | - sentry(app, { |
182 | | - dsn: '__DSN__', // or process.env.SENTRY_DSN or Bun.env.SENTRY_DSN |
183 | | - tracesSampleRate: 1.0, |
184 | | - }), |
185 | | -); |
186 | | - |
187 | | -// ... your routes and other middleware |
188 | | - |
189 | | -serve(app); |
190 | | -``` |
191 | | - |
192 | | -## Setup (Deno) |
193 | | - |
194 | | -### 1. Install Peer Dependency |
195 | | - |
196 | | -Additionally to `@sentry/hono`, install the `@sentry/deno` package: |
197 | | - |
198 | | -```bash |
199 | | -npm install --save @sentry/deno |
200 | | -``` |
201 | | - |
202 | | -Make sure the installed version always stays in sync. The `@sentry/deno` package is a required peer dependency when using `@sentry/hono/deno`. |
203 | | -You won't import `@sentry/deno` directly in your code, but it needs to be installed in your project. |
204 | | - |
205 | | -### 2. Initialize Sentry in your Hono app |
206 | | - |
207 | | -Initialize the Sentry Hono middleware as early as possible in your app, then serve it with `Deno.serve`: |
208 | | - |
209 | | -```ts |
210 | | -import { Hono } from 'hono'; |
211 | | -import { sentry } from '@sentry/hono/deno'; |
212 | | - |
213 | | -const app = new Hono(); |
214 | | - |
215 | | -// Initialize Sentry middleware right after creating the app |
216 | | -app.use( |
217 | | - sentry(app, { |
218 | | - dsn: '__DSN__', // or Deno.env.get('SENTRY_DSN') |
219 | | - tracesSampleRate: 1.0, |
220 | | - }), |
221 | | -); |
222 | | - |
223 | | -// ... your routes and other middleware |
224 | | - |
225 | | -Deno.serve(app.fetch); |
226 | | -``` |
227 | | - |
228 | | -Run your app with the permissions Sentry needs to read environment variables and send events: |
229 | | - |
230 | | -```bash |
231 | | -deno run --allow-net --allow-env --allow-read app.ts |
232 | | -``` |
233 | | - |
234 | | -## Filtering errors |
235 | | - |
236 | | -By default, `@sentry/hono` captures 5xx errors and plain `Error` objects, and ignores 3xx/4xx HTTP errors (redirects, not-found, bad request, etc.). |
237 | | - |
238 | | -Use `shouldHandleError` to override this on a per-error basis: |
239 | | - |
240 | | -```ts |
241 | | -app.use( |
242 | | - sentry(app, { |
243 | | - dsn: '__DSN__', |
244 | | - shouldHandleError(error) { |
245 | | - const status = (error as { status?: number })?.status; |
246 | | - // Capture 401/403 in addition to the default 5xx errors |
247 | | - return status === 401 || status === 403 || typeof status !== 'number' || status >= 500; |
248 | | - }, |
249 | | - }), |
250 | | -); |
251 | | -``` |
252 | | - |
253 | | -Return `true` to capture the error, `false` to suppress it. |
| 23 | +- [Report a bug](https://github.com/getsentry/sentry-javascript/issues/new/choose) |
| 24 | +- [Contributing](https://github.com/getsentry/sentry-javascript/blob/develop/CONTRIBUTING.md) |
0 commit comments