mirror of
https://github.com/oven-sh/bun
synced 2026-02-10 19:08:50 +00:00
## Summary This PR fixes WebSocket to correctly emit an `error` event before the `close` event when the handshake fails (e.g., 302 redirects, non-101 status codes, missing headers). Fixes #14338 ## Problem Previously, when a WebSocket connection failed during handshake (like receiving a 302 redirect or connecting to a non-WebSocket server), Bun would only emit a `close` event. This behavior differed from the WHATWG WebSocket specification and other runtimes (browsers, Node.js with `ws`, Deno) which emit both `error` and `close` events. ## Solution Modified `WebSocket::didFailWithErrorCode()` in `WebSocket.cpp` to pass `isConnectionError = true` for all handshake failure error codes, ensuring an error event is dispatched before the close event when the connection is in the CONNECTING state. ## Changes - Updated error handling in `src/bun.js/bindings/webcore/WebSocket.cpp` to emit error events for handshake failures - Added comprehensive test coverage in `test/regression/issue/14338.test.ts` ## Test Coverage The test file includes: 1. **Negative test**: 302 redirect response - verifies error event is emitted 2. **Negative test**: Non-WebSocket HTTP server - verifies error event is emitted 3. **Positive test**: Successful WebSocket connection - verifies NO error event is emitted All tests pass with the fix applied. 🤖 Generated with [Claude Code](https://claude.ai/code) --------- Co-authored-by: Claude Bot <claude-bot@bun.sh> Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: Jarred Sumner <jarred@jarredsumner.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
3.9 KiB
3.9 KiB