Skip to content

Commit ae4928e

Browse files
committed
feat: Improve Hono docs to cover errors + Node.js
1 parent fcae333 commit ae4928e

File tree

2 files changed

+92
-52
lines changed

2 files changed

+92
-52
lines changed

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

Lines changed: 73 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,45 @@ 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+
```typescript {filename:index.ts}
46+
import { Hono, HTTPException } from "hono";
47+
import * as Sentry from "@sentry/node";
48+
49+
const app = new Hono()
50+
// Add an onError hook to report unhandled exceptions to Sentry.
51+
.onError((err, c) => {
52+
// Report _all_ unhandled errors.
53+
Sentry.captureException(err);
54+
if (err instanceof HTTPException) {
55+
return err.getResponse()
56+
}
57+
// Or just report errors which are not instances of HTTPException
58+
// Sentry.captureException(err);
59+
return c.json({ error: "Internal server error" }, 500)
60+
})
61+
// Your routes...
62+
app.get("/", () => {
63+
// ...
64+
});
65+
66+
export default app;
67+
```
68+
69+
3170
### Setup On Cloudflare Workers
3271

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

65-
66-
67104
```typescript {filename:index.ts}
68-
import { Hono } from "hono";
105+
import { Hono, HTTPException } from "hono";
69106
import * as Sentry from "@sentry/cloudflare";
70107

71-
const app = new Hono();
108+
Sentry.init({
109+
dsn: "___PUBLIC_DSN___",
110+
72111

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

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

82-
147+
83148
// Adds request headers and IP for users, for more info visit:
84149
// https://docs.sentry.io/platforms/javascript/guides/hono/configuration/options/#sendDefaultPii
85150
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)