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)
|
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 {
|
||||||
|
try {
|
||||||
this.#socket.close()
|
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[]
|
||||||
|
@ -81,6 +81,7 @@ export class Nostr {
|
|||||||
|
|
||||||
// Handle messages on this connection.
|
// Handle messages on this connection.
|
||||||
conn.on("message", async (msg) => {
|
conn.on("message", async (msg) => {
|
||||||
|
try {
|
||||||
if (msg.kind === IncomingKind.Event) {
|
if (msg.kind === IncomingKind.Event) {
|
||||||
this.#eventCallback?.(
|
this.#eventCallback?.(
|
||||||
{
|
{
|
||||||
@ -94,6 +95,13 @@ export class Nostr {
|
|||||||
this.#noticeCallback?.(msg.notice, this)
|
this.#noticeCallback?.(msg.notice, this)
|
||||||
} else {
|
} else {
|
||||||
const err = new ProtocolError(`invalid message ${msg}`)
|
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
|
||||||
|
Loading…
Reference in New Issue
Block a user