From 2882d95c54973bbe6d25e295535dc4a4f0bf1a4a Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Tue, 4 Aug 2026 11:51:17 -0700 Subject: [PATCH] Fix account refresh rep setting and refactor RPC error logging. --- src/lib/rpc/index.ts | 14 ++++++++++---- src/lib/wallet/refresh.ts | 5 +---- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/lib/rpc/index.ts b/src/lib/rpc/index.ts index e05192e..90f6e24 100644 --- a/src/lib/rpc/index.ts +++ b/src/lib/rpc/index.ts @@ -67,11 +67,17 @@ export class Rpc { }, 10000) try { const res = await fetch(req) - const { status, statusText } = res + const status = res.status + const statusText = res.statusText ? ` ${res.statusText}` : '' + if (status !== 200) { + throw new Error(`${status} ${statusText}`, { cause: JSON.stringify(res) }) + } const resBody = await res.json() - const { error, message } = resBody - if (status !== 200 || error != null) { - throw new Error(`${status} ${statusText}`, { cause: { error, message } }) + const code = resBody.code ? `${resBody.code} ` : '' + const error = resBody.error ? `${resBody.error}\n` : '' + const message = resBody.message ?? '' + if (error) { + throw new Error(`${code}${error}${message}`, { cause: JSON.stringify(resBody) }) } return resBody } catch (err) { diff --git a/src/lib/wallet/refresh.ts b/src/lib/wallet/refresh.ts index aaa286f..24e4dcf 100644 --- a/src/lib/wallet/refresh.ts +++ b/src/lib/wallet/refresh.ts @@ -42,17 +42,14 @@ export async function _refresh (wallet: Wallet, rpc: unknown, from: unknown, to: } const { balances } = await rpc.post('accounts_balances', data) as { balances?: { [address: string]: { balance: string, receivable: string } } } const { frontiers } = await rpc.post('accounts_frontiers', data) as { frontiers: { [address: string]: string } } - const { representatives } = await rpc.post('accounts_representatives', data) as { representatives: { [address: string]: string } } const { blocks } = await rpc.post('blocks_info', { json_block: true, hashes: Object.values(frontiers) }) as BlockInfo for (const account of accounts.values()) { account.balance = balances?.[account.address]?.balance ?? 0 account.receivable = balances?.[account.address]?.receivable ?? 0 - account.representative = representatives?.[account.address] if (frontiers[account.address] != null) { - const { representative: accountRepresentative } = await rpc.post('account_representative', { account: account.address }) as { representative: string } - account.representative ??= accountRepresentative account.frontier = frontiers[account.address] const { subtype, contents: { balance, link, previous, representative, signature } } = blocks[account.frontier] + account.representative = representative const frontierBlock = new Block(account, balance, previous, representative) if (typeof frontierBlock[subtype] !== 'function') { throw new TypeError('Unknown frontier block subtype', { cause: subtype }) -- 2.52.0