11import { dummyLogger , type Logger } from "ts-log" ;
22import TTLCache from "@isaacs/ttlcache" ;
33import WebSocket from "isomorphic-ws" ;
4- import type { Request } from "../protocol.js" ;
4+ import type { Request , Response } from "../protocol.js" ;
55import { ResilientWebSocket } from "./ResillientWebSocket.js" ;
66
77// Maintains multiple redundant WebSocket connections for reliability
@@ -72,6 +72,18 @@ export class WebSocketPool {
7272 this . logger . info ( `Using ${ numConnections } redundant WebSocket connections` ) ;
7373 }
7474
75+ /**
76+ * Checks for error responses in JSON messages and throws appropriate errors
77+ */
78+ private handleErrorMessages ( data : string ) : void {
79+ const message = JSON . parse ( data ) as Response ;
80+ if ( message . type === "subscriptionError" ) {
81+ throw new Error ( `Subscription error: ${ message } ` ) ;
82+ } else if ( message . type === "error" ) {
83+ throw new Error ( `Error: ${ message . error } ` ) ;
84+ }
85+ }
86+
7587 /**
7688 * Handles incoming websocket messages by deduplicating identical messages received across
7789 * multiple connections before forwarding to registered handlers
@@ -93,6 +105,11 @@ export class WebSocketPool {
93105 // Haven't seen this message, cache it and forward to handlers
94106 this . cache . set ( cacheKey , true ) ;
95107
108+ // Check for errors in JSON responses
109+ if ( typeof data === "string" ) {
110+ this . handleErrorMessages ( data ) ;
111+ }
112+
96113 for ( const handler of this . messageListeners ) {
97114 handler ( data ) ;
98115 }
@@ -151,8 +168,6 @@ export class WebSocketPool {
151168 for ( const rws of this . rwsPool ) {
152169 rws . onReconnect = ( ) => { } ;
153170 rws . onError = ( ) => { } ;
154- }
155- for ( const rws of this . rwsPool ) {
156171 rws . closeWebSocket ( ) ;
157172 }
158173 this . rwsPool = [ ] ;
0 commit comments