handle all errors in error callbacks

This commit is contained in:
ennmichael
2023-02-28 22:47:08 +01:00
parent 9c52be315e
commit 6ef866feb3
2 changed files with 42 additions and 25 deletions

View File

@ -81,19 +81,27 @@ export class Nostr {
// Handle messages on this connection.
conn.on("message", async (msg) => {
if (msg.kind === IncomingKind.Event) {
this.#eventCallback?.(
{
signed: msg.signed,
subscriptionId: msg.subscriptionId,
raw: msg.raw,
},
this
)
} else if (msg.kind === IncomingKind.Notice) {
this.#noticeCallback?.(msg.notice, this)
} else {
const err = new ProtocolError(`invalid message ${msg}`)
try {
if (msg.kind === IncomingKind.Event) {
this.#eventCallback?.(
{
signed: msg.signed,
subscriptionId: msg.subscriptionId,
raw: msg.raw,
},
this
)
} else if (msg.kind === IncomingKind.Notice) {
this.#noticeCallback?.(msg.notice, this)
} 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)
}
})
@ -317,7 +325,7 @@ export interface Filters {
export type EventCallback = (params: EventParams, 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 {
signed: SignedEvent