Hono

Learn how to set up Hono with Sentry.

This guide explains how to set up Sentry in your Hono application.

If you don't already have an account and a Sentry project established, sign up for Sentry for free, then return to this page.

In addition to capturing errors, you can monitor interactions between multiple services or applications by enabling tracing. You can also collect and analyze performance profiles from real users with profiling. Note that profiling currently only works in the Node.js runtime.

Select which Sentry features you'd like to install in addition to Error Monitoring to get the corresponding installation and configuration instructions below.

For setup instructions on Cloudflare Workers, see the dedicated Hono on Cloudflare guide.

This guide assumes you're using the Node.js runtime. Start by installing the Sentry dependency:

Copied
npm install @sentry/node --save

Now bind an onError hook to report unhandled exceptions to Sentry:

Copied
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);
  })

  // Bind global context via Hono middleware
  .use((c, next) => {
    Sentry.setUser({
      email: c.session.user.email,
    });

    Sentry.setTag("project_id", c.session.projectId);

    return next();
  })

  // Your routes...
  .get("/", () => {
    // ...
  });

This snippet includes an intentional error, so you can test that everything is working as soon as you set it up.

Copied
app.get("/debug-sentry", async (c) => {
  throw new Error("My first Sentry error!");
});

To view and resolve the recorded error, log into sentry.io and select your project. Clicking on the error's title will open a page where you can see detailed information and mark it as resolved.

Was this helpful?
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").