From c30523278a87a614b0bebc8d70bff976351db646 Mon Sep 17 00:00:00 2001 From: Nedunchezhiyan-M Date: Wed, 8 Apr 2026 23:18:21 +0530 Subject: [PATCH] Improve WebSocket error messages for CONNECTING state The send() and ping() methods throw "INVALID_STATE_ERR" which is an opaque error code that doesn't tell the developer what went wrong. Updated to descriptive messages that explain the actual issue, matching the style used by browser WebSocket implementations. --- packages/react-native/Libraries/WebSocket/WebSocket.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/react-native/Libraries/WebSocket/WebSocket.js b/packages/react-native/Libraries/WebSocket/WebSocket.js index 36aafbbbab17..01c991a152f5 100644 --- a/packages/react-native/Libraries/WebSocket/WebSocket.js +++ b/packages/react-native/Libraries/WebSocket/WebSocket.js @@ -181,7 +181,9 @@ class WebSocket extends EventTarget { send(data: string | ArrayBuffer | ArrayBufferView | Blob): void { if (this.readyState === this.CONNECTING) { - throw new Error('INVALID_STATE_ERR'); + throw new Error( + "Failed to execute 'send' on 'WebSocket': Still in CONNECTING state", + ); } if (data instanceof Blob) { @@ -208,7 +210,9 @@ class WebSocket extends EventTarget { ping(): void { if (this.readyState === this.CONNECTING) { - throw new Error('INVALID_STATE_ERR'); + throw new Error( + "Failed to execute 'ping' on 'WebSocket': Still in CONNECTING state", + ); } NativeWebSocketModule.ping(this._socketId);