mirror of
https://github.com/simplex-chat/simplex-chat.git
synced 2026-10-15 11:10:30 +00:00
nodejs: send error events to bot (#7691)
* nodejs: send error events to bot * version --------- Co-authored-by: Evgeny @ SimpleX Chat <259188159+evgeny-simplex@users.noreply.github.com>
This commit is contained in:
co-authored by
Evgeny @ SimpleX Chat
parent
599076d7a2
commit
eda94a505d
@@ -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": [
|
||||
|
||||
@@ -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<void> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user