/** * 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 }