Skip to content

Commit 09b443a

Browse files
authored
Merge pull request #96 from JJ-Cro/master
fix(2.3.1): improve error handling in WebSocket client, fix circular error
2 parents 4178797 + 4edc12b commit 09b443a

File tree

4 files changed

+18
-5
lines changed

4 files changed

+18
-5
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "kucoin-api",
3-
"version": "2.3.0",
3+
"version": "2.3.1",
44
"description": "Complete & robust Node.js SDK for Kucoin's REST APIs and WebSockets, with TypeScript & strong end to end tests.",
55
"scripts": {
66
"clean": "rm -rf dist",

src/WebsocketAPIClient.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,17 @@ export class WebsocketAPIClient {
279279
console.info(new Date(), 'ws has authenticated ', data?.wsKey);
280280
})
281281
.on('exception', (data) => {
282-
console.error(new Date(), 'ws exception: ', JSON.stringify(data));
282+
try {
283+
// Blind JSON.stringify can fail on circular references
284+
console.error(
285+
new Date(),
286+
'ws exception: ',
287+
JSON.stringify(data),
288+
// JSON.stringify({ ...data, target: 'WebSocket' }),
289+
);
290+
} catch {
291+
console.error(new Date(), 'ws exception: ', data);
292+
}
283293
});
284294
}
285295
}

src/lib/BaseWSClient.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,10 @@ export abstract class BaseWebsocketClient<
461461
return { wsKey, ws: this.wsStore.getWs(wsKey)! };
462462
}
463463

464-
if (this.wsStore.isConnectionAttemptInProgress(wsKey)) {
464+
if (
465+
// Important: don't check for RECONNECTING here, or this clashes with reconnectWithDelay()!
466+
this.wsStore.isConnectionState(wsKey, WsConnectionStateEnum.CONNECTING)
467+
) {
465468
this.logger.error(
466469
'Refused to connect to ws, connection attempt already active',
467470
{ ...WS_LOGGER_CATEGORY, wsKey },

0 commit comments

Comments
 (0)