Skip to content

Commit 1e1c2ea

Browse files
committed
feat: add error handler, bump ver
1 parent 79b685a commit 1e1c2ea

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

lazer/sdk/js/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pythnetwork/pyth-lazer-sdk",
3-
"version": "0.1.2",
3+
"version": "0.2.0",
44
"description": "Pyth Lazer SDK",
55
"publishConfig": {
66
"access": "public"

lazer/sdk/js/src/socket/WebSocketPool.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { dummyLogger, type Logger } from "ts-log";
22
import TTLCache from "@isaacs/ttlcache";
33
import WebSocket from "isomorphic-ws";
4-
import type { Request } from "../protocol.js";
4+
import type { Request, Response } from "../protocol.js";
55
import { 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

Comments
 (0)