handle all errors in error callbacks

This commit is contained in:
ennmichael 2023-02-28 22:47:08 +01:00
parent 9c52be315e
commit 6ef866feb3
No known key found for this signature in database
GPG Key ID: 6E6E183431A26AF7
2 changed files with 42 additions and 25 deletions

View File

@ -47,14 +47,7 @@ export class Conn {
const msg = await parseIncomingMessage(value) const msg = await parseIncomingMessage(value)
this.#msgCallback?.(msg) this.#msgCallback?.(msg)
} catch (err) { } catch (err) {
if (err instanceof ProtocolError) { this.#errorCallback?.(err)
this.#errorCallback?.(err)
} else {
// TODO Not sure if this is the best idea.
// Investigate what WebSocket does if the callback throws?
// Either way it seems like the best idea is to have `onError` called on all types of errors
throw err
}
} }
}) })
@ -65,6 +58,10 @@ export class Conn {
} }
this.#pending = [] this.#pending = []
}) })
this.#socket.addEventListener("error", (err) => {
this.#errorCallback?.(err)
})
} }
on(on: "message", cb: IncomingMessageCallback): void on(on: "message", cb: IncomingMessageCallback): void
@ -84,11 +81,23 @@ export class Conn {
this.#pending.push(msg) this.#pending.push(msg)
return return
} }
this.#socket.send(serializeOutgoingMessage(msg)) try {
this.#socket.send(serializeOutgoingMessage(msg), (err) => {
if (err !== undefined && err !== null) {
this.#errorCallback?.(err)
}
})
} catch (err) {
this.#errorCallback?.(err)
}
} }
close(): void { close(): void {
this.#socket.close() try {
this.#socket.close()
} catch (err) {
this.#errorCallback?.(err)
}
} }
} }
@ -160,7 +169,7 @@ export interface OutgoingCloseSubscription {
} }
type IncomingMessageCallback = (message: IncomingMessage) => unknown type IncomingMessageCallback = (message: IncomingMessage) => unknown
type ErrorCallback = (error: ProtocolError) => unknown type ErrorCallback = (error: unknown) => unknown
interface RawFilters { interface RawFilters {
ids?: string[] ids?: string[]

View File

@ -81,19 +81,27 @@ export class Nostr {
// Handle messages on this connection. // Handle messages on this connection.
conn.on("message", async (msg) => { conn.on("message", async (msg) => {
if (msg.kind === IncomingKind.Event) { try {
this.#eventCallback?.( if (msg.kind === IncomingKind.Event) {
{ this.#eventCallback?.(
signed: msg.signed, {
subscriptionId: msg.subscriptionId, signed: msg.signed,
raw: msg.raw, subscriptionId: msg.subscriptionId,
}, raw: msg.raw,
this },
) this
} else if (msg.kind === IncomingKind.Notice) { )
this.#noticeCallback?.(msg.notice, this) } else if (msg.kind === IncomingKind.Notice) {
} else { this.#noticeCallback?.(msg.notice, this)
const err = new ProtocolError(`invalid message ${msg}`) } else {
const err = new ProtocolError(`invalid message ${msg}`)
try {
this.#errorCallback?.(err, this)
} catch (err) {
// Don't propagate errors from the error callback.
}
}
} catch (err) {
this.#errorCallback?.(err, this) this.#errorCallback?.(err, this)
} }
}) })
@ -317,7 +325,7 @@ export interface Filters {
export type EventCallback = (params: EventParams, nostr: Nostr) => unknown export type EventCallback = (params: EventParams, nostr: Nostr) => unknown
export type NoticeCallback = (notice: string, nostr: Nostr) => unknown export type NoticeCallback = (notice: string, nostr: Nostr) => unknown
export type ErrorCallback = (error: ProtocolError, nostr: Nostr) => unknown export type ErrorCallback = (error: unknown, nostr: Nostr) => unknown
export interface EventParams { export interface EventParams {
signed: SignedEvent signed: SignedEvent