Skip to content

Commit a37a031

Browse files
authored
feat: Improve Hono docs to cover errors + Node.js (#13936)
Update the hono include to be semi-correct (uncertain if used currently).
1 parent f581125 commit a37a031

File tree

2 files changed

+95
-52
lines changed

2 files changed

+95
-52
lines changed

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

Lines changed: 76 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,48 @@ Select which Sentry features you'd like to install in addition to Error Monitori
2828

2929
Since Hono is a framework designed to run in all kinds of JavaScript runtimes, different setups for different platforms are outlined below.
3030

31+
### Setup On Node.js
32+
33+
```bash {tabTitle:npm}
34+
npm install @sentry/node --save
35+
```
36+
37+
```bash {tabTitle:yarn}
38+
yarn add @sentry/node
39+
```
40+
41+
```bash {tabTitle:pnpm}
42+
pnpm add @sentry/node
43+
```
44+
45+
From there, you'll need to bind an `onError` hook to report unhandled exceptions to Sentry:
46+
47+
```typescript {filename:index.ts}
48+
import { Hono, HTTPException } from "hono";
49+
import * as Sentry from "@sentry/node";
50+
51+
const app = new Hono()
52+
// Add an onError hook to report unhandled exceptions to Sentry.
53+
.onError((err, c) => {
54+
// Report _all_ unhandled errors.
55+
Sentry.captureException(err);
56+
if (err instanceof HTTPException) {
57+
return err.getResponse()
58+
}
59+
// Or just report errors which are not instances of HTTPException
60+
// Sentry.captureException(err);
61+
return c.json({ error: "Internal server error" }, 500)
62+
})
63+
// Your routes...
64+
app.get("/", () => {
65+
// ...
66+
});
67+
68+
export default app;
69+
```
70+
71+
Note: We don't currently support automatic tracing instrumentation for Hono within the Node.js adapter.
72+
3173
### Setup On Cloudflare Workers
3274

3375
```bash {tabTitle:npm}
@@ -62,24 +104,50 @@ compatibility_flags = ["nodejs_als"]
62104
Next, wrap your handler with the `withSentry` function. This will initialize the SDK and hook into the
63105
environment. Note that you can turn off almost all side effects using the respective options.
64106

65-
66-
67107
```typescript {filename:index.ts}
68-
import { Hono } from "hono";
108+
import { Hono, HTTPException } from "hono";
69109
import * as Sentry from "@sentry/cloudflare";
70110

71-
const app = new Hono();
111+
Sentry.init({
112+
dsn: "___PUBLIC_DSN___",
113+
72114

73-
// Your routes...
74-
app.get("/", () => {
75-
// ...
115+
// Adds request headers and IP for users, for more info visit:
116+
// https://docs.sentry.io/platforms/javascript/guides/hono/configuration/options/#sendDefaultPii
117+
sendDefaultPii: true,
118+
119+
// ___PRODUCT_OPTION_START___ performance
120+
// Set tracesSampleRate to 1.0 to capture 100% of spans for tracing.
121+
// Learn more at
122+
// https://docs.sentry.io/platforms/javascript/configuration/options/#traces-sample-rate
123+
tracesSampleRate: 1.0,
124+
// ___PRODUCT_OPTION_END___ performance
76125
});
77126

127+
const app = new Hono()
128+
// Add an onError hook to report unhandled exceptions to Sentry.
129+
.onError((err, c) => {
130+
// Report _all_ unhandled errors.
131+
Sentry.captureException(err);
132+
if (err instanceof HTTPException) {
133+
return err.getResponse()
134+
}
135+
// Or just report errors which are not instances of HTTPException
136+
// Sentry.captureException(err);
137+
return c.json({ error: "Internal server error" }, 500)
138+
})
139+
// Your routes...
140+
app.get("/", () => {
141+
// ...
142+
});
143+
144+
// Wrap your Worker binding with Sentry to ensure tracing instrumentation is enabled,
145+
// and Sentry telemetry is flushed at the end of requests.
78146
export default Sentry.withSentry(
79147
(env) => ({
80148
dsn: "___PUBLIC_DSN___",
81149

82-
150+
83151
// Adds request headers and IP for users, for more info visit:
84152
// https://docs.sentry.io/platforms/javascript/guides/hono/configuration/options/#sendDefaultPii
85153
sendDefaultPii: true,
Lines changed: 19 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,23 @@
1-
```javascript {tabTitle:CommonJS}
2-
// Require this first!
3-
require("./instrument");
4-
5-
// Now require other modules
6-
const Sentry = require("@sentry/node");
7-
const Hapi = require('@hapi/hapi');
8-
9-
const init = async () => {
10-
const server = Hapi.server({
11-
port: 3030,
12-
host: 'localhost',
13-
});
14-
15-
// All your routes etc.
16-
17-
await Sentry.setupHapiErrorHandler(server);
18-
19-
await server.start();
20-
console.log('Server running on %s', server.info.uri);
21-
};
22-
23-
init();
24-
```
25-
26-
```javascript {tabTitle:ESM}
27-
// Import this first!
28-
import "./instrument";
29-
30-
// Now import other modules
1+
```javascript
312
import * as Sentry from "@sentry/node";
32-
import Hapi from "@hapi/hapi";
33-
34-
const init = async () => {
35-
const server = Hapi.server({
36-
port: 3030,
37-
host: 'localhost',
3+
import { Hono, HTTPException } from "hono";
4+
5+
const app = new Hono()
6+
// Add an onError hook to report unhandled exceptions to Sentry.
7+
.onError((err, c) => {
8+
// Report _all_ unhandled errors.
9+
Sentry.captureException(err);
10+
if (err instanceof HTTPException) {
11+
return err.getResponse()
12+
}
13+
// Or just report errors which are not instances of HTTPException
14+
// Sentry.captureException(err);
15+
return c.json({ error: "Internal server error" }, 500)
16+
})
17+
// Your routes...
18+
app.get("/", () => {
19+
// ...
3820
});
3921

40-
// All your routes etc.
41-
42-
await Sentry.setupHapiErrorHandler(server);
43-
44-
await server.start();
45-
};
46-
47-
init();
22+
export default app;
4823
```

0 commit comments

Comments
 (0)