Skip to content

feat(node): Auto-register Hapi error handler on server start - #23461

Merged
mydea merged 2 commits into
developfrom
fn/hapi-error-handler
Aug 26, 2026
Merged

mydea merged 2 commits into
developfrom
fn/hapi-error-handler

Conversation

@mydea

@mydea mydea commented Aug 20, 2026

Copy link
Copy Markdown
Member

The Hapi error handler is now registered automatically when the server boots, so setupHapiErrorHandler no longer needs to be called. This closes the last piece of the Hapi integration that still required a manual setup step.

How it works

The tracing side of the Hapi integration already runs through orchestrion diagnostics channels in @sentry/server-utils (injected into @hapi/hapi's lib/server.js). This reuses the same mechanism for the error handler: two new channels are injected on the server start and initialize methods. Orchestrion publishes the live server instance as ctx.self on those channels, which is enough to attach the request/error listener that sets the transaction name and captures the exception.

This also adds a shouldHandleError callback like other integrations have to decide what to capture to Sentry or not.

Decisions

  • Hook both start and initialize. start() calls the private _core._start() (which internally runs _initialize), so the public initialize method never fires on the common await server.start() path. Hooking both covers the standard path plus test/serverless flows that only call initialize() + inject().
  • Attach once per shared emitter. Hapi shares one event emitter (core.events) across the root server and every plugin clone, so a single listener covers all requests. Attachment is made idempotent via a non-enumerable marker on that emitter, so start+initialize, plugin clones, and any lingering manual setupHapiErrorHandler call never stack up multiple listeners.
  • setupHapiErrorHandler stays as a deprecated delegate rather than being removed or hard no-op'd. Auto-registration only fires when orchestrion is active (the default in v11), so keeping the function functional preserves the escape hatch; it is idempotent, so calling it alongside auto-registration is harmless.
  • The handler logic and its structural types moved from @sentry/node into @sentry/server-utils (integrations/hapi/) so both the auto-registration and the delegate share one implementation.

Auto-registration is exercised end-to-end by dropping the manual call from the node integration-test scenario and the e2e app; a new unit suite covers the attach behaviour (single listener, idempotency, plugin-clone dedup, transaction naming, and the skip cases).

Comment thread packages/server-utils/src/integrations/hapi/index.ts
Comment thread packages/server-utils/src/orchestrion/config/hapi.ts
@github-actions

github-actions Bot commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

size-limit report 📦

Path Size % Change Change
@sentry/browser 28.57 kB - -
@sentry/browser - with treeshaking flags 26.92 kB - -
@sentry/browser - with treeshaking flags tracing without tracing 26.82 kB - -
@sentry/browser (incl. Tracing) 48.58 kB - -
@sentry/browser (incl. Tracing + Span Streaming) 48.6 kB - -
@sentry/browser (incl. Tracing, Profiling) 51.51 kB - -
@sentry/browser (incl. Tracing, Replay) 88.04 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 77.44 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 92.75 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 105.44 kB - -
@sentry/browser (incl. Feedback) 45.81 kB - -
@sentry/browser (incl. sendFeedback) 33.36 kB - -
@sentry/browser (incl. FeedbackAsync) 38.46 kB - -
@sentry/browser (incl. Metrics) 29.51 kB - -
@sentry/browser (incl. Logs) 29.8 kB - -
@sentry/browser (incl. Metrics & Logs) 30.43 kB - -
@sentry/react 30.31 kB - -
@sentry/react (incl. Tracing) 50.79 kB - -
@sentry/vue 35.69 kB - -
@sentry/vue (incl. Tracing) 50.82 kB - -
@sentry/svelte 28.59 kB - -
CDN Bundle 30.36 kB - -
CDN Bundle (incl. Tracing) 49.07 kB - -
CDN Bundle (incl. Logs, Metrics) 32.56 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 50.95 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) 72.98 kB - -
CDN Bundle (incl. Tracing, Replay) 86.56 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 88.44 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 92.33 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 94.25 kB - -
CDN Bundle - uncompressed 89.97 kB - -
CDN Bundle (incl. Tracing) - uncompressed 146.69 kB - -
CDN Bundle (incl. Logs, Metrics) - uncompressed 96.26 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 152.38 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 225.36 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 266.12 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 271.79 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 279.81 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 285.48 kB - -
@sentry/nextjs (client) 53.33 kB - -
@sentry/sveltekit (client) 49.03 kB - -
@sentry/core/server 65.08 kB - -
@sentry/core/browser 52.38 kB - -
@sentry/node 121.86 kB +0.31% +370 B 🔺
@sentry/node/import (ESM hook with diagnostics-channel injection) 85.22 kB +0.04% +26 B 🔺
@sentry/node - without tracing 87.96 kB +0.41% +357 B 🔺
@sentry/aws-serverless 96.14 kB +0.42% +400 B 🔺
@sentry/cloudflare (withSentry) - minified 199.5 kB - -
@sentry/cloudflare (withSentry) 495.46 kB - -

View base workflow run

@mydea mydea changed the title feat(node): Auto-register Hapi error handler on server start feat(node)!: Auto-register Hapi error handler on server start Aug 20, 2026
@mydea mydea changed the title feat(node)!: Auto-register Hapi error handler on server start feat(node): Auto-register Hapi error handler on server start Aug 20, 2026
@mydea
mydea marked this pull request as ready for review August 20, 2026 10:51
@mydea
mydea requested review from a team as code owners August 20, 2026 10:51
@mydea
mydea requested review from JPeer264, chargome, isaacs and s1gr1d and removed request for a team August 20, 2026 10:51
@mydea
mydea marked this pull request as draft August 20, 2026 11:33
@mydea
mydea marked this pull request as ready for review August 21, 2026 06:30

@JPeer264 JPeer264 left a comment

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.

Tests are failing, but after a rebase I think ok again

@mydea
mydea force-pushed the fn/hapi-error-handler branch from 98d690d to 0682600 Compare August 26, 2026 07:26
Comment thread packages/server-utils/src/integrations/hapi/hapi-error-handler.ts Outdated
@mydea
mydea force-pushed the fn/hapi-error-handler branch from 0682600 to 5055cd1 Compare August 26, 2026 08:15

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 5055cd1. Configure here.

Comment thread packages/server-utils/src/integrations/hapi/hapi-utils.ts
@mydea
mydea force-pushed the fn/hapi-error-handler branch from 5055cd1 to 43ad0e8 Compare August 26, 2026 08:54
@mydea
mydea merged commit bef02f7 into develop Aug 26, 2026
387 of 389 checks passed
@mydea
mydea deleted the fn/hapi-error-handler branch August 26, 2026 10:50
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.

2 participants