Skip to content

feat: Improve Hono docs to cover errors + Node.js #13936

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 76 additions & 8 deletions docs/platforms/javascript/guides/hono/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,48 @@ Select which Sentry features you'd like to install in addition to Error Monitori

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

### Setup On Node.js

```bash {tabTitle:npm}
npm install @sentry/node --save
```

```bash {tabTitle:yarn}
yarn add @sentry/node
```

```bash {tabTitle:pnpm}
pnpm add @sentry/node
```

From there, you'll need to bind an `onError` hook to report unhandled exceptions to Sentry:

```typescript {filename:index.ts}
import { Hono, HTTPException } from "hono";
import * as Sentry from "@sentry/node";

const app = new Hono()
// Add an onError hook to report unhandled exceptions to Sentry.
.onError((err, c) => {
// Report _all_ unhandled errors.
Sentry.captureException(err);
if (err instanceof HTTPException) {
return err.getResponse()
}
// Or just report errors which are not instances of HTTPException
// Sentry.captureException(err);
return c.json({ error: "Internal server error" }, 500)
})
// Your routes...
app.get("/", () => {
// ...
});

export default app;
```

Note: We don't currently support automatic tracing instrumentation for Hono within the Node.js adapter.

### Setup On Cloudflare Workers

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



```typescript {filename:index.ts}
import { Hono } from "hono";
import { Hono, HTTPException } from "hono";
import * as Sentry from "@sentry/cloudflare";

const app = new Hono();
Sentry.init({
dsn: "___PUBLIC_DSN___",


// Your routes...
app.get("/", () => {
// ...
// Adds request headers and IP for users, for more info visit:
// https://docs.sentry.io/platforms/javascript/guides/hono/configuration/options/#sendDefaultPii
sendDefaultPii: true,

// ___PRODUCT_OPTION_START___ performance
// Set tracesSampleRate to 1.0 to capture 100% of spans for tracing.
// Learn more at
// https://docs.sentry.io/platforms/javascript/configuration/options/#traces-sample-rate
tracesSampleRate: 1.0,
// ___PRODUCT_OPTION_END___ performance
});

const app = new Hono()
// Add an onError hook to report unhandled exceptions to Sentry.
.onError((err, c) => {
// Report _all_ unhandled errors.
Sentry.captureException(err);
if (err instanceof HTTPException) {
return err.getResponse()
}
// Or just report errors which are not instances of HTTPException
// Sentry.captureException(err);
return c.json({ error: "Internal server error" }, 500)
})
// Your routes...
app.get("/", () => {
// ...
});

// Wrap your Worker binding with Sentry to ensure tracing instrumentation is enabled,
// and Sentry telemetry is flushed at the end of requests.
export default Sentry.withSentry(
(env) => ({
dsn: "___PUBLIC_DSN___",


// Adds request headers and IP for users, for more info visit:
// https://docs.sentry.io/platforms/javascript/guides/hono/configuration/options/#sendDefaultPii
sendDefaultPii: true,
Expand Down
63 changes: 19 additions & 44 deletions platform-includes/getting-started-use/javascript.hono.mdx
Original file line number Diff line number Diff line change
@@ -1,48 +1,23 @@
```javascript {tabTitle:CommonJS}
// Require this first!
require("./instrument");

// Now require other modules
const Sentry = require("@sentry/node");
const Hapi = require('@hapi/hapi');

const init = async () => {
const server = Hapi.server({
port: 3030,
host: 'localhost',
});

// All your routes etc.

await Sentry.setupHapiErrorHandler(server);

await server.start();
console.log('Server running on %s', server.info.uri);
};

init();
```

```javascript {tabTitle:ESM}
// Import this first!
import "./instrument";

// Now import other modules
```javascript
import * as Sentry from "@sentry/node";
import Hapi from "@hapi/hapi";

const init = async () => {
const server = Hapi.server({
port: 3030,
host: 'localhost',
import { Hono, HTTPException } from "hono";

const app = new Hono()
// Add an onError hook to report unhandled exceptions to Sentry.
.onError((err, c) => {
// Report _all_ unhandled errors.
Sentry.captureException(err);
if (err instanceof HTTPException) {
return err.getResponse()
}
// Or just report errors which are not instances of HTTPException
// Sentry.captureException(err);
return c.json({ error: "Internal server error" }, 500)
})
// Your routes...
app.get("/", () => {
// ...
});

// All your routes etc.

await Sentry.setupHapiErrorHandler(server);

await server.start();
};

init();
export default app;
```