diff --git a/packages/simplex-chat-nodejs/package.json b/packages/simplex-chat-nodejs/package.json index f3ab195b9b..a482bafae8 100644 --- a/packages/simplex-chat-nodejs/package.json +++ b/packages/simplex-chat-nodejs/package.json @@ -1,6 +1,6 @@ { "name": "simplex-chat", - "version": "7.1.0-beta.6.1", + "version": "7.1.0-beta.6.2", "main": "dist/index.js", "types": "dist/index.d.ts", "files": [ diff --git a/packages/simplex-chat-nodejs/src/api.ts b/packages/simplex-chat-nodejs/src/api.ts index 8e0c5a545d..95d942ebf1 100644 --- a/packages/simplex-chat-nodejs/src/api.ts +++ b/packages/simplex-chat-nodejs/src/api.ts @@ -80,6 +80,11 @@ export type DbConfig = connectionString: string } +function chatErrorEvent(err: unknown): ChatEvent { + if (err instanceof core.ChatAPIError && err.chatError) return {type: "chatError", chatError: err.chatError} + throw err +} + function dbConfigToMigrateArgs(db: DbConfig): [string, string] { switch (db.type) { case "sqlite": @@ -171,7 +176,7 @@ export class ChatApi { private async runEventsLoop(): Promise { while (this.receiveEvents) { try { - const event = await this.recvChatEvent() + const event = await this.recvChatEvent().catch(chatErrorEvent) if (!event) continue const subs = this.subscribers[event.type] if (subs) { @@ -194,12 +199,7 @@ export class ChatApi { } } } catch(err) { - const e = err as core.ChatAPIError - if ("chatError" in e) { - console.log("Chat error", e.chatError) - } else { - console.log("Invalid event", e) - } + console.log("Invalid event", err) } } } diff --git a/packages/simplex-chat-nodejs/tests/api.unit.test.ts b/packages/simplex-chat-nodejs/tests/api.unit.test.ts index b6b92bf93b..8bfecc3347 100644 --- a/packages/simplex-chat-nodejs/tests/api.unit.test.ts +++ b/packages/simplex-chat-nodejs/tests/api.unit.test.ts @@ -110,6 +110,16 @@ describe("startChat lifecycle", () => { await chat.stopChat() }) + it("passes event errors to chatError subscribers", async () => { + const chat = await chatWithResponses({type: "chatStarted"}, {type: "chatStopped"}) + const chatError = {type: "errorStore", storeError: {type: "connectionNotFound", agentConnId: "Y29ubg=="}} as T.ChatError + jest.mocked(core.chatRecvMsgWait).mockRejectedValueOnce(new core.ChatAPIError("Chat event error (see chatError property)", chatError)) + const received = new Promise(resolve => chat.on("chatError", event => resolve(event))) + await chat.startChat() + await expect(received).resolves.toEqual({type: "chatError", chatError}) + await chat.stopChat() + }) + it("receives with a 500 ms wait", async () => { const chat = await chatWithResponses() await chat.recvChatEvent()