Skip to content

Read handshake status from public API instead of parsing library error strings #1118

Description

@aqandrew

Background

handshakeStatus (src/websocket/utils.ts) recovers the HTTP status of a failed
WebSocket/SSE handshake by parsing the error message text from two libraries:

  • wsUnexpected server response: <code>
  • eventsourceNon-200 status code (<code>)

Neither string is a public contract; both are plain template literals
(ws/lib/websocket.js and eventsource/src/EventSource.ts). If either reword on
a version bump, handshakeStatus returns undefined, every unrecoverable status
becomes a retry-forever connection_error, and CoderApi.is404Error stops
falling back to SSE.

A real-library test (test/unit/websocket/utils.test.ts) now guards against a
silent reword by failing in CI, but parsing internal strings is still fragile.

Proposal

Read the status from the libraries' public event APIs instead of the message:

  • SSE: eventsource puts the status on ErrorEvent.code (documented public
    API). SseConnection.createErrorEvent (src/websocket/sseConnection.ts)
    currently reads only message and error and drops code; surface it.
  • WebSocket: ws exposes the same on its unexpected-response event, which
    also suppresses the string path once handled.

Once the status comes from the events, handshakeStatus's string parsing can be
narrowed or removed.

Acceptance

  • Handshake status is derived from ErrorEvent.code / the ws
    unexpected-response event, not the error message text.
  • Unrecoverable-HTTP handling and the SSE 404 fallback keep working.

Filed by Coder Agents on behalf of @aqandrew, as a follow-up to PR #1100.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions