Files
peardata/client/errors.js
T
Raven Scott 015d92a257
Release rolling / release (push) Has been cancelled
CI / test (push) Has been cancelled
first commit
2026-07-18 16:17:38 -04:00

31 lines
659 B
JavaScript

/**
* Normalize protomux-rpc / DHT errors for UI.
*/
/**
* @param {unknown} err
* @returns {Error}
*/
export function unwrapError(err) {
let cur = err
let depth = 0
while (cur?.cause && depth < 5) {
cur = cur.cause
depth++
}
return cur instanceof Error ? cur : new Error(String(cur?.message || cur || 'Unknown error'))
}
/**
* @param {unknown} err
* @param {string} [method]
*/
export function normalizeRpcError(err, method) {
const root = unwrapError(err)
const e = new Error(root.message || String(err))
e.code = root.code || err?.code || 'RPC_ERROR'
e.method = method || root.method || null
e.cause = err
return e
}