Skip to content

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

Draft
mydea wants to merge 4 commits into
developfrom
fn/hapi-error-handler
Draft

feat(node): Auto-register Hapi error handler on server start#23461
mydea wants to merge 4 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).

mydea and others added 2 commits August 20, 2026 11:25
The Hapi error handler now registers itself automatically when the server
starts, so `setupHapiErrorHandler` no longer needs to be called. The handler
logic moves to `@sentry/server-utils` and is wired via orchestrion channels on
`@hapi/hapi`'s `start`/`initialize` methods; `setupHapiErrorHandler` is kept as
a deprecated, idempotent delegate for backwards compatibility.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

@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 2 potential issues.

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 c88eb54. Configure here.

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 30.3 kB - -
@sentry/browser - with treeshaking flags 28.47 kB - -
@sentry/browser - with treeshaking flags tracing without tracing 26.81 kB - -
@sentry/browser (incl. Tracing) 48.58 kB - -
@sentry/browser (incl. Tracing + Span Streaming) 48.59 kB - -
@sentry/browser (incl. Tracing, Profiling) 51.46 kB - -
@sentry/browser (incl. Tracing, Replay) 87.98 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 77.36 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 92.7 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 105.39 kB - -
@sentry/browser (incl. Feedback) 47.65 kB - -
@sentry/browser (incl. sendFeedback) 35.13 kB - -
@sentry/browser (incl. FeedbackAsync) 40.28 kB - -
@sentry/browser (incl. Metrics) 31.24 kB - -
@sentry/browser (incl. Logs) 31.52 kB - -
@sentry/browser (incl. Metrics & Logs) 32.15 kB - -
@sentry/react 32.09 kB - -
@sentry/react (incl. Tracing) 50.77 kB - -
@sentry/vue 35.34 kB - -
@sentry/vue (incl. Tracing) 50.54 kB - -
@sentry/svelte 30.33 kB - -
CDN Bundle 31.61 kB - -
CDN Bundle (incl. Tracing) 48.9 kB - -
CDN Bundle (incl. Logs, Metrics) 33.8 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 50.82 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) 74.31 kB - -
CDN Bundle (incl. Tracing, Replay) 86.48 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 88.31 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 92.19 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 94.12 kB - -
CDN Bundle - uncompressed 93.84 kB - -
CDN Bundle (incl. Tracing) - uncompressed 146.75 kB - -
CDN Bundle (incl. Logs, Metrics) - uncompressed 100.14 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 152.44 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 229.08 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 266.01 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 271.68 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 279.71 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 285.37 kB - -
@sentry/nextjs (client) 53.29 kB - -
@sentry/sveltekit (client) 49 kB - -
@sentry/core/server 65.38 kB - -
@sentry/core/browser 51.72 kB - -
@sentry/node 117.72 kB +0.29% +337 B 🔺
@sentry/node/import (ESM hook with diagnostics-channel injection) 0 B added added
@sentry/node - without tracing 81.91 kB +0.03% +21 B 🔺
@sentry/aws-serverless 91.32 kB +0.03% +23 B 🔺
@sentry/cloudflare (withSentry) - minified 213.98 kB - -
@sentry/cloudflare (withSentry) 528.93 kB - -

View base workflow run

This reverts commit c88eb54.
@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
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.

1 participant