fix(packages/api): misleading errors being thrown

This commit is contained in:
PalmDevs
2024-07-23 21:00:18 +07:00
parent 4834685186
commit 94d0dcc32b

View File

@@ -53,23 +53,34 @@ export class ClientWebSocketManager {
}
}, this.timeout)
this.#socket.on('open', () => {
this.disconnected = false
clearTimeout(timeout)
this.#listen()
rs()
})
this.#socket.on('error', err => {
clearTimeout(timeout)
const errorBeforeReadyHandler = (err: Error) => {
cleanup()
throw err
})
}
this.#socket.on('close', (code, reason) => {
const closeBeforeReadyHandler = (code: number, reason: Buffer) => {
clearTimeout(timeout)
this._handleDisconnect(code, reason.toString())
throw new Error('WebSocket connection closed before ready')
})
}
const readyHandler = () => {
this.disconnected = false
cleanup()
this.#listen()
rs()
}
const cleanup = () => {
this.#socket.off('open', readyHandler)
this.#socket.off('close', closeBeforeReadyHandler)
this.#socket.off('error', errorBeforeReadyHandler)
clearTimeout(timeout)
}
this.#socket.on('open', readyHandler)
this.#socket.on('error', errorBeforeReadyHandler)
this.#socket.on('close', closeBeforeReadyHandler)
} catch (e) {
rj(e)
}