Skip to content

fix: forward close/error reason to channels in triggerChanError - #29

Merged
mandarini merged 1 commit into
mainfrom
fix/forward-channel-error-reason
Apr 28, 2026
Merged

mandarini merged 1 commit into
mainfrom
fix/forward-channel-error-reason

Conversation

@mandarini

@mandarini mandarini commented Apr 28, 2026 •

Copy link
Copy Markdown
Collaborator

What kind of change does this PR introduce?

Bug fix.

What is the current behavior?

When a transport-level failure occurs (socket close, heartbeat timeout, connection error), Socket.triggerChanError() calls channel.trigger(CHANNEL_EVENTS.error) with no payload. As a result, every Channel.onError(callback) listener receives reason === undefined, even though the calling sites (onConnClose(event), onConnError(error), heartbeatTimeout()) all have the underlying close or error event in scope.

This bubbles all the way up to consumers. In @supabase/realtime-js, the user's channel.subscribe((status, err) => ...) callback fires with status === 'CHANNEL_ERROR' and err === undefined, so applications cannot distinguish between a heartbeat timeout, an abnormal websocket close, an invalid JWT, or a network error. See supabase/supabase-js#1734 for the user-facing report.

What is the new behavior?

triggerChanError now accepts an optional reason and forwards it as the payload to channel.trigger(CHANNEL_EVENTS.error, reason). The three callers pass what they already have:

  • onConnClose(event) forwards the CloseEvent
  • onConnError(error) forwards the transport Event
  • heartbeatTimeout() synthesizes new Error("heartbeat timeout") since no DOM event is in scope

Channel.trigger(event, payload, ref, joinRef) already accepts a payload, so no signature change is needed in channel.js. Existing onError(callback => ...) listeners that ignore the argument keep working unchanged; listeners that look at the argument now get a meaningful value instead of undefined.

Additional context

  • No public API change: the only behavioral difference is that Channel.onError listeners receive a real reason instead of undefined. Consumers that previously did nothing with the argument are unaffected.
  • The companion fix in @supabase/realtime-js adds a normalizer that maps Error | CloseEvent | Event | string | undefined to a real Error with cause, so end users see messages like "socket closed: 1006 (Network Failure)" and can still inspect the raw event.
  • Tests:
    • Updated socket_test.js assertions for the existing onConnClose / onConnError cases to verify the close/error event is forwarded as the second argument to phx_error.
    • Added a new test in the heartbeatCallback describe block asserting that heartbeatTimeout() triggers phx_error with an Error("heartbeat timeout") payload.
    • Full JS suite passes locally (npx jest, 208 tests).
  • Files in priv/static/ are not committed here; the assets.yml workflow rebuilds them from assets/js/phoenix/* on merge.

@mandarini
mandarini merged commit 9b327f5 into main Apr 28, 2026
13 checks passed
@mandarini
mandarini deleted the fix/forward-channel-error-reason branch April 28, 2026 08:42
@edgurgel edgurgel mentioned this pull request Nov 23, 2026
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