Skip to content

feat(hono): Add shouldHandleError as middleware option#21205

Merged
s1gr1d merged 7 commits into
developfrom
sig/hono-add-shouldHandleError
May 28, 2026
Merged

feat(hono): Add shouldHandleError as middleware option#21205
s1gr1d merged 7 commits into
developfrom
sig/hono-add-shouldHandleError

Conversation

@s1gr1d
Copy link
Copy Markdown
Member

@s1gr1d s1gr1d commented May 28, 2026

Adds the error filtering logic to the Hono sentry() middleware. It already exists in the to-be deprecated honoIntegration: #17743

isExpectedError was renamed to defaultShouldHandleError so the name is matching with the user-provided parameter. I also removed the special catching within the middleware, as it's just "duplicated code". The error bubbles through the Hono context anyway - it's a bit simpler now.

Closes #21204

@s1gr1d s1gr1d requested a review from a team as a code owner May 28, 2026 07:24
@s1gr1d s1gr1d requested review from mydea and nicohrubec and removed request for a team May 28, 2026 07:24
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same file as before

Copy link
Copy Markdown
Member

@chargome chargome left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

if (!isExpectedError(error)) {
// Error capture is handled by `responseHandler` via `context.error`, so this
// wrapper only sets span status and rethrows (no `captureException`).
if (defaultShouldHandleError(error)) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just confirming that here we do not use the user-provided shouldHandleError, was that intentional? I think it is fine tbh. Just e.g. if a user wants to catch 400s as errors then span status would not be set to error here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll only set the span as "errored" when our default conditions apply.
Users could potentially want to report 200 as error and it would be wrong to set the span as "errored".

*/
export function responseHandler(context: Context): void {
if (context.error && !isExpectedError(context.error)) {
export function responseHandler(context: Context, shouldHandleError?: (error: unknown) => boolean): void {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

l: should this maybe take the type directly from what we set in SentryHonoMiddlewareOptions?

* Determines whether an error should be captured and sent to Sentry.
* Uses `shouldHandleError` when provided, otherwise falls back to `defaultShouldHandleError`.
*/
export function shouldCaptureError(error: unknown, shouldHandleError?: (error: unknown) => boolean): boolean {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

l: maybe that's just me but would putting that in a variable not be sufficient here instead of delegating that to a separate method? 😅 I also don't see this being reused anywhere

if (!isExpectedError(error)) {
// Error capture is handled by `responseHandler` via `context.error`, so this
// wrapper only sets span status and rethrows (no `captureException`).
if (defaultShouldHandleError(error)) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m: is this intentionally defaultShouldHandleError and not shouldCaptureError?

Comment thread CHANGELOG.md
This release promotes the `@sentry/tanstackstart-react` SDK to beta. For details on how to use it, check out the
[Sentry TanStack Start SDK docs](https://docs.sentry.io/platforms/javascript/guides/tanstackstart-react/). Please reach out on
[GitHub](https://github.com/getsentry/sentry-javascript/issues/new/choose) if you have any feedback or concerns.
The `sentry()` middleware now accepts a `shouldHandleError` callback to control which errors are captured and sent to Sentry. By default, 3xx/4xx HTTP errors are ignored and 5xx errors and plain `Error` objects are captured. Return `true` from the callback to capture an error, `false` to suppress it.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

l: maybe an example would help to understand how this is to be used?

* **Note:** You must initialize Sentry separately before using this middleware. Typically, this is done by calling `Sentry.init()` in an `instrument.ts` file and loading it via the Node `--import` flag.
*/
export const sentry = <E extends Env>(app: Hono<E>): MiddlewareHandler => {
export const sentry = <E extends Env>(app: Hono<E>, options?: SentryHonoMiddlewareOptions): MiddlewareHandler => {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m: should this take HonoNodeOptions (which then I think would need to extend SentryHonoMiddlewareOptions) like in the other middlewares?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because in the Node runtime the options are passed to the init in the external instrumentation file.

Comment thread packages/hono/src/shared/middlewareHandlers.ts Outdated
@s1gr1d s1gr1d enabled auto-merge (squash) May 28, 2026 11:47
@s1gr1d s1gr1d merged commit 556bcb3 into develop May 28, 2026
48 checks passed
@s1gr1d s1gr1d deleted the sig/hono-add-shouldHandleError branch May 28, 2026 12:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Hono] Add shouldCaptureError as middleware option

3 participants