handle all errors in error callbacks
This commit is contained in:
parent
9c52be315e
commit
6ef866feb3
@ -47,14 +47,7 @@ export class Conn {
|
||||
const msg = await parseIncomingMessage(value)
|
||||
this.#msgCallback?.(msg)
|
||||
} catch (err) {
|
||||
if (err instanceof ProtocolError) {
|
||||
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
|
||||
}
|
||||
this.#errorCallback?.(err)
|
||||
}
|
||||
})
|
||||
|
||||
@ -65,6 +58,10 @@ export class Conn {
|
||||
}
|
||||
this.#pending = []
|
||||
})
|
||||
|
||||
this.#socket.addEventListener("error", (err) => {
|
||||
this.#errorCallback?.(err)
|
||||
})
|
||||
}
|
||||
|
||||
on(on: "message", cb: IncomingMessageCallback): void
|
||||
@ -84,11 +81,23 @@ export class Conn {
|
||||
this.#pending.push(msg)
|
||||
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 {
|
||||
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 ErrorCallback = (error: ProtocolError) => unknown
|
||||
type ErrorCallback = (error: unknown) => unknown
|
||||
|
||||
interface RawFilters {
|
||||
ids?: string[]
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user