Skip to content

Commit 6352f2c

Browse files
docs(js): Create Hono Quick Start guide (#14504)
<!-- Use this checklist to make sure your PR is ready for merge. You may delete any sections you don't need. --> ## DESCRIBE YOUR PR Updated the Hono getting started guide into a quick start guide. The guide is now based on our Node.js content, like Koa or Hapi. Closes #14290 ## 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 --> - [x] 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) ## EXTRA RESOURCES - [Sentry Docs contributor guide](https://docs.sentry.io/contributing/) --------- Co-authored-by: Charly Gomez <[email protected]>
1 parent 4621cfb commit 6352f2c

File tree

6 files changed

+168
-92
lines changed

6 files changed

+168
-92
lines changed
Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
---
22
title: Hono
3-
description: Learn how to set up Hono with Sentry.
3+
description: Learn how to manually set up Sentry in your Hono app and capture your first errors.
44
sdk: sentry.javascript.hono
5+
fallbackGuide: javascript.node
56
categories:
67
- javascript
78
- server
@@ -10,40 +11,10 @@ categories:
1011

1112
<PlatformContent includePath="getting-started-primer" />
1213

13-
This guide explains how to set up Sentry in your Hono application.
14+
<Alert type="warning" title="Important">
15+
This guide assumes you're using the **Node.js runtime** for Hono. For setup
16+
instructions on Cloudflare Workers, see our [Hono on Cloudflare
17+
guide](/platforms/javascript/guides/cloudflare/frameworks/hono/).
18+
</Alert>
1419

15-
If you don't already have an account and a Sentry project established, sign up for [Sentry](https://sentry.io/signup/) for free, then return to this page.
16-
17-
## Features
18-
19-
In addition to capturing errors, you can monitor interactions between multiple services or applications by [enabling tracing](/concepts/key-terms/tracing/). You can also collect and analyze performance profiles from real users with [profiling](/product/explore/profiling/). Note that profiling currently only works in the Node.js runtime.
20-
21-
Select which Sentry features you'd like to install in addition to Error Monitoring to get the corresponding installation and configuration instructions below.
22-
23-
<OnboardingOptionButtons
24-
options={["error-monitoring", "performance", "profiling", "logs"]}
25-
/>
26-
27-
## Setup
28-
29-
For setup instructions on Cloudflare Workers, see the dedicated [Hono on Cloudflare guide](/platforms/javascript/guides/cloudflare/frameworks/hono/).
30-
31-
This guide assumes you're using the Node.js runtime. Start by installing the Sentry dependency:
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-
<PlatformContent includePath="getting-started-config" platform="javascript.hono" />
46-
47-
## Verify
48-
49-
<PlatformContent includePath="getting-started-verify" platform="javascript.hono" />
20+
<PlatformContent includePath="getting-started-node" />
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
### Report Unhandled Exceptions
2+
3+
Next, bind an `onError` hook to report unhandled exceptions to Sentry:
4+
5+
```javascript
6+
const app = new Hono()
7+
// Add an onError hook to report unhandled exceptions to Sentry.
8+
.onError((err, c) => {
9+
// Report _all_ unhandled errors.
10+
Sentry.captureException(err);
11+
if (err instanceof HTTPException) {
12+
return err.getResponse();
13+
}
14+
// Or just report errors which are not instances of HTTPException
15+
// Sentry.captureException(err);
16+
return c.json({ error: "Internal server error" }, 500);
17+
})
18+
19+
// Bind global context via Hono middleware
20+
.use((c, next) => {
21+
Sentry.setUser({
22+
email: c.session.user.email,
23+
});
24+
25+
Sentry.setTag("project_id", c.session.projectId);
26+
27+
return next();
28+
})
29+
30+
// Your routes...
31+
.get("/", () => {
32+
// ...
33+
});
34+
```
Lines changed: 90 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,91 @@
1-
Now bind an `onError` hook to report unhandled exceptions to Sentry:
2-
3-
```javascript
4-
const app = new Hono()
5-
// Add an onError hook to report unhandled exceptions to Sentry.
6-
.onError((err, c) => {
7-
// Report _all_ unhandled errors.
8-
Sentry.captureException(err);
9-
if (err instanceof HTTPException) {
10-
return err.getResponse()
11-
}
12-
// Or just report errors which are not instances of HTTPException
13-
// Sentry.captureException(err);
14-
return c.json({ error: "Internal server error" }, 500)
15-
})
16-
17-
// Bind global context via Hono middleware
18-
.use((c, next) => {
19-
Sentry.setUser({
20-
email: c.session.user.email,
21-
})
22-
23-
Sentry.setTag("project_id", c.session.projectId);
24-
25-
return next();
26-
})
27-
28-
// Your routes...
29-
.get("/", () => {
30-
// ...
31-
});
1+
```javascript {tabTitle:CommonJS} {filename: instrument.js}
2+
const Sentry = require("@sentry/node");
3+
// ___PRODUCT_OPTION_START___ profiling
4+
const { nodeProfilingIntegration } = require("@sentry/profiling-node");
5+
// ___PRODUCT_OPTION_END___ profiling
6+
7+
// Ensure to call this before requiring any other modules!
8+
Sentry.init({
9+
dsn: "___PUBLIC_DSN___",
10+
11+
// Adds request headers and IP for users, for more info visit:
12+
// https://docs.sentry.io/platforms/javascript/guides/hono/configuration/options/#sendDefaultPii
13+
sendDefaultPii: true,
14+
// ___PRODUCT_OPTION_START___ profiling
15+
16+
integrations: [
17+
// Add our Profiling integration
18+
nodeProfilingIntegration(),
19+
],
20+
// ___PRODUCT_OPTION_END___ profiling
21+
// ___PRODUCT_OPTION_START___ performance
22+
23+
// Set tracesSampleRate to 1.0 to capture 100%
24+
// of transactions for tracing.
25+
// We recommend adjusting this value in production
26+
// Learn more at
27+
// https://docs.sentry.io/platforms/javascript/guides/hono/configuration/options/#tracesSampleRate
28+
tracesSampleRate: 1.0,
29+
// ___PRODUCT_OPTION_END___ performance
30+
// ___PRODUCT_OPTION_START___ profiling
31+
32+
// Set profilesSampleRate to 1.0 to profile 100%
33+
// of sampled transactions.
34+
// This is relative to tracesSampleRate
35+
// Learn more at
36+
// https://docs.sentry.io/platforms/javascript/guides/hono/configuration/options/#profilesSampleRate
37+
profilesSampleRate: 1.0,
38+
// ___PRODUCT_OPTION_END___ profiling
39+
// ___PRODUCT_OPTION_START___ logs
40+
41+
// Enable logs to be sent to Sentry
42+
enableLogs: true,
43+
// ___PRODUCT_OPTION_END___ logs
44+
});
45+
```
46+
47+
```javascript {tabTitle:ESM} {filename: instrument.mjs}
48+
import * as Sentry from "@sentry/node";
49+
// ___PRODUCT_OPTION_START___ profiling
50+
import { nodeProfilingIntegration } from "@sentry/profiling-node";
51+
// ___PRODUCT_OPTION_END___ profiling
52+
53+
// Ensure to call this before importing any other modules!
54+
Sentry.init({
55+
dsn: "___PUBLIC_DSN___",
56+
57+
// Adds request headers and IP for users, for more info visit:
58+
// https://docs.sentry.io/platforms/javascript/guides/hono/configuration/options/#sendDefaultPii
59+
sendDefaultPii: true,
60+
// ___PRODUCT_OPTION_START___ profiling
61+
62+
integrations: [
63+
// Add our Profiling integration
64+
nodeProfilingIntegration(),
65+
],
66+
// ___PRODUCT_OPTION_END___ profiling
67+
// ___PRODUCT_OPTION_START___ performance
68+
69+
// Set tracesSampleRate to 1.0 to capture 100%
70+
// of transactions for tracing.
71+
// We recommend adjusting this value in production
72+
// Learn more at
73+
// https://docs.sentry.io/platforms/javascript/guides/hono/configuration/options/#tracesSampleRate
74+
tracesSampleRate: 1.0,
75+
// ___PRODUCT_OPTION_END___ performance
76+
// ___PRODUCT_OPTION_START___ profiling
77+
78+
// Set profilesSampleRate to 1.0 to profile 100%
79+
// of sampled transactions.
80+
// This is relative to tracesSampleRate
81+
// Learn more at
82+
// https://docs.sentry.io/platforms/javascript/guides/hono/configuration/options/#profilesSampleRate
83+
profilesSampleRate: 1.0,
84+
// ___PRODUCT_OPTION_END___ profiling
85+
// ___PRODUCT_OPTION_START___ logs
86+
87+
// Enable logs to be sent to Sentry
88+
enableLogs: true,
89+
// ___PRODUCT_OPTION_END___ logs
90+
});
3291
```

platform-includes/getting-started-node/javascript.mdx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ When running your application in ESM mode, use the [--import](https://nodejs.org
5353
node --import ./instrument.mjs app.mjs
5454
```
5555

56+
<PlatformContent includePath="getting-started-capture-errors" />
57+
5658
## Step 3: Add Readable Stack Traces With Source Maps (Optional)
5759

5860
The stack traces in your Sentry errors probably won't look like your actual code. To fix this, upload your source maps to Sentry.
@@ -70,7 +72,7 @@ Let's test your setup and confirm that Sentry is working correctly and sending d
7072

7173
### View Captured Data in Sentry
7274

73-
Now, head over to your project on [Sentry.io](https://sentry.io/) to view the collected data (it takes a couple of moments for the data to appear).
75+
Finally, head over to your project on [Sentry.io](https://sentry.io/) to view the collected data (it takes a couple of moments for the data to appear).
7476

7577
<PlatformContent includePath="getting-started-verify-locate-data" />
7678

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,10 @@
11
```javascript
2-
import * as Sentry from "@sentry/node";
3-
import { Hono, HTTPException } from "hono";
2+
// Require this first!
3+
require("./instrument");
44

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-
// ...
20-
});
5+
// Now require other modules
6+
const { serve } = require("@hono/node-server");
7+
const { Hono } = require("hono");
218

22-
export default app;
9+
// Your application code goes here
2310
```
Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,32 @@
1-
This snippet includes an intentional error, so you can test that everything is working as soon as you set it up.
1+
### Issues
22

3-
```typescript
4-
app.get("/debug-sentry", async (c) => {
3+
First, let's verify that Sentry captures errors and creates issues in your Sentry project. Add the following code snippet to your main application file, adding a route that triggers an error that Sentry will capture:
4+
5+
```javascript
6+
app.get("/debug-sentry", () => {
57
throw new Error("My first Sentry error!");
68
});
79
```
810

9-
To view and resolve the recorded error, log into [sentry.io](https://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.
11+
<OnboardingOption optionId="performance">
12+
13+
### Tracing
14+
15+
To test your tracing configuration, update the previous code snippet by starting a trace to measure the time it takes for the execution of your code:
16+
17+
```javascript
18+
app.get("/debug-sentry", async () => {
19+
await Sentry.startSpan(
20+
{
21+
op: "test",
22+
name: "My First Test Transaction",
23+
},
24+
async () => {
25+
await new Promise((resolve) => setTimeout(resolve, 100)); // Wait for 100ms
26+
throw new Error("My first Sentry error!");
27+
}
28+
);
29+
});
30+
```
31+
32+
</OnboardingOption>

0 commit comments

Comments
 (0)