Skip to content

Commit cf094ef

Browse files
authored
docs(hono): Document shouldHandleError option (#17923)
## DESCRIBE YOUR PR Documents: getsentry/sentry-javascript#21205 Adds "Hono Features" to the sidebar: <img width="297" height="220" alt="image" src="https://github.com/user-attachments/assets/7bf2b777-df43-4b97-8911-9c273d233b38" /> ## IS YOUR CHANGE URGENT? Help us prioritize incoming PRs by letting us know when the change needs to go live. - [ ] Urgent deadline (GA date, etc.): <!-- ENTER DATE HERE --> - [ ] Other deadline: <!-- ENTER DATE HERE --> - [ ] None: Not urgent, can wait up to 1 week+ ## SLA - Teamwork makes the dream work, so please add a reviewer to your PRs. - Please give the docs team up to 1 week to review your PR unless you've added an urgent due date to it. Thanks in advance for your help! ## PRE-MERGE CHECKLIST *Make sure you've checked the following before merging your changes:* - [ ] Checked Vercel preview for correctness, including links - [ ] PR was reviewed and approved by any necessary SMEs (subject matter experts) - [ ] PR was reviewed and approved by a member of the [Sentry docs team](https://github.com/orgs/getsentry/teams/docs)
1 parent de23c1b commit cf094ef

3 files changed

Lines changed: 114 additions & 0 deletions

File tree

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
---
2+
title: Error Handler
3+
description: "Learn how Sentry's Hono SDK captures errors and how to customize which errors are sent to Sentry."
4+
---
5+
6+
The `sentry()` middleware automatically captures unhandled errors from Hono's `onError` handler.
7+
8+
## Default Behavior
9+
10+
The default logic checks whether the error carries a numeric `status` property (as Hono's `HTTPException` and similar error types do).
11+
12+
The middleware captures:
13+
14+
- **5xx errors** — anything with a numeric `status >= 500`
15+
- **Non-HTTP errors** — errors without a numeric `status` property, including standard `Error` instances, non-object throws, and `null`
16+
17+
The middleware ignores:
18+
19+
- **3xx–4xx errors** — anything with a numeric `status` in the range 300–499 (redirects, client errors)
20+
21+
Use `shouldHandleError` when you need to capture errors outside this default range, for example to track 401 and 403 responses as security signals.
22+
23+
## Using `shouldHandleError`
24+
25+
Pass a `shouldHandleError` callback to the `sentry()` middleware. The callback receives the raw error as `unknown` and must return `true` to capture it or `false` to suppress it.
26+
27+
```typescript {tabTitle:Cloudflare Workers} {filename:index.ts} {mdExpandTabs}
28+
import { Hono } from "hono";
29+
import { sentry } from "@sentry/hono/cloudflare";
30+
31+
const app = new Hono();
32+
33+
app.use(
34+
sentry(app, {
35+
dsn: "___PUBLIC_DSN___",
36+
shouldHandleError(error) {
37+
const status = (error as { status?: number })?.status;
38+
// Capture 401/403 in addition to the default 5xx errors
39+
return (
40+
status === 401 ||
41+
status === 403 ||
42+
typeof status !== "number" ||
43+
status >= 500
44+
);
45+
},
46+
})
47+
);
48+
49+
export default app;
50+
```
51+
52+
```typescript {tabTitle:Node.js} {filename:app.ts}
53+
import { Hono } from "hono";
54+
import { serve } from "@hono/node-server";
55+
import { sentry } from "@sentry/hono/node";
56+
57+
const app = new Hono();
58+
59+
app.use(
60+
sentry(app, {
61+
shouldHandleError(error) {
62+
const status = (error as { status?: number })?.status;
63+
// Capture 401/403 in addition to the default 5xx errors
64+
return (
65+
status === 401 ||
66+
status === 403 ||
67+
typeof status !== "number" ||
68+
status >= 500
69+
);
70+
},
71+
})
72+
);
73+
74+
// Your routes here
75+
76+
serve(app);
77+
```
78+
79+
```typescript {tabTitle:Bun} {filename:index.ts}
80+
import { Hono } from "hono";
81+
import { sentry } from "@sentry/hono/bun";
82+
83+
const app = new Hono();
84+
85+
app.use(
86+
sentry(app, {
87+
dsn: "___PUBLIC_DSN___",
88+
shouldHandleError(error) {
89+
const status = (error as { status?: number })?.status;
90+
// Capture 401/403 in addition to the default 5xx errors
91+
return (
92+
status === 401 ||
93+
status === 403 ||
94+
typeof status !== "number" ||
95+
status >= 500
96+
);
97+
},
98+
})
99+
);
100+
101+
export default app;
102+
```
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
title: Hono Features
3+
description: "Learn how Sentry's Hono SDK exposes features for first-class integration with Hono."
4+
excerpt: ""
5+
---
6+
7+
The Sentry Hono SDK offers Hono-specific features for first-class integration with the framework.
8+
9+
<PageGrid />

docs/platforms/javascript/guides/hono/index.mdx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,8 @@ export default app;
357357

358358
By default, Sentry captures exceptions from Hono's `onError` handler, excluding errors with 3xx or 4xx status codes. No additional configuration is needed for this.
359359

360+
To control which errors are captured, use the <PlatformLink to="/features/error-handler/">`shouldHandleError`</PlatformLink> option.
361+
360362
### Add Readable Stack Traces With Source Maps (Optional)
361363

362364
<PlatformContent includePath="getting-started-sourcemaps-short-version" />
@@ -379,6 +381,7 @@ Now's a good time to customize your setup and look into more advanced topics:
379381

380382
- Explore [practical guides](/guides/) on what to monitor, log, track, and investigate after setup
381383
- Learn how to <PlatformLink to="/usage">manually capture errors</PlatformLink>
384+
- Explore <PlatformLink to="/features">Hono-specific features</PlatformLink> like custom error filtering
382385
- Continue to <PlatformLink to="/configuration">customize your configuration</PlatformLink>
383386
- Get familiar with [Sentry's product features](/product) like tracing, insights, and alerts
384387

0 commit comments

Comments
 (0)