@@ -28,6 +28,45 @@ Select which Sentry features you'd like to install in addition to Error Monitori
2828
2929Since 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"]
62101Next, wrap your handler with the ` withSentry ` function. This will initialize the SDK and hook into the
63102environment. 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" ;
69106import * 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.
78143export 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 ,
0 commit comments