]> git.codecow.com Git - libnemo.git/commitdiff
Simplify multi-account RPC response parsing.
authorChris Duncan <chris@codecow.com>
Mon, 10 Aug 2026 04:49:49 +0000 (21:49 -0700)
committerChris Duncan <chris@codecow.com>
Mon, 10 Aug 2026 04:49:49 +0000 (21:49 -0700)
src/lib/rpc/accounts_balances.ts
src/lib/rpc/accounts_frontiers.ts
src/lib/rpc/blocks_info.ts

index bce825a26b0957167a2a8aadc8cbcc4c5a5b65e2..5b18218b9184aa03715e6d5e7ca0554e07f22cfc 100644 (file)
@@ -19,38 +19,21 @@ export function accounts_balances (body: unknown): AccountsBalancesResponse {
                const response: AccountsBalancesResponse = Object.create(null)
 
                if ('errors' in body && body.errors != null && typeof body.errors === 'object') {
+                       const errors: Record<string, string> = response.errors ?? Object.create(null)
+                       response.errors = errors
                        for (const [address, error] of Object.entries(body.errors) as [string, unknown][]) {
-                               try {
-                                       if (!Account.isValid(address)) {
-                                               throw new RpcError('Invalid account address')
-                                       }
-                                       if (typeof error === 'string') {
-                                               throw new RpcError(error)
-                                       }
-                               } catch (err: any) {
-                                       const errors = response.errors ?? Object.create(null)
-                                       response.errors = errors
-                                       errors[address] = err?.message
+                               if (Account.isValid(address) && typeof error === 'string') {
+                                       errors[address] = error
                                }
                        }
                }
 
                if ('balances' in body && body.balances != null && typeof body.balances === 'object') {
+                       const balances: Record<string, AccountBalanceResponse> = response.balances ?? Object.create(null)
+                       response.balances = balances
                        for (const [address, data] of Object.entries(body.balances) as [string, unknown][]) {
-                               try {
-                                       if (!Account.isValid(address)) {
-                                               throw new RpcError('Invalid account address')
-                                       }
-                                       if (data == null || typeof data !== 'object') {
-                                               throw new RpcError('Invalid account balance data')
-                                       }
-                                       const balances = response.balances ?? Object.create(null)
-                                       response.balances = balances
+                               if (Account.isValid(address)) {
                                        balances[address] = account_balance(data)
-                               } catch (err: any) {
-                                       const errors = response.errors ?? Object.create(null)
-                                       response.errors = errors
-                                       errors[address] = err?.message
                                }
                        }
                }
index 173eae49952e63dfab79b2dcf36a56692cfa16dc..0bf74691ce299b35f4c2894577e95116ec659961 100644 (file)
@@ -15,52 +15,30 @@ export type AccountsFrontiersResponse = {
 }
 
 export function accounts_frontiers (body: unknown): AccountsFrontiersResponse {
-       const response: AccountsFrontiersResponse = Object.create(null)
-       if (body == null || typeof body !== 'object') {
-               return response
-       }
-       const frontiers = 'frontiers' in body ? body.frontiers : null
-       const errors = 'errors' in body ? body.errors : null
+       if (body != null && typeof body === 'object') {
+               const response: AccountsFrontiersResponse = Object.create(null)
 
-       if (frontiers != null) {
-               if (typeof frontiers === 'object') {
-                       for (const [address, frontier] of Object.entries(frontiers) as [string, unknown][]) {
-                               try {
-                                       if (!Account.isValid(address)) {
-                                               throw new RpcError('Invalid account address')
-                                       }
-                                       if (hex.is(frontier, 64)) {
-                                               const frontiers = response.frontiers ?? Object.create(null)
-                                               response.frontiers = frontiers
-                                               frontiers[address] = frontier
-                                       }
-                               } catch (err: any) {
-                                       const errors = response.errors ?? Object.create(null)
-                                       response.errors = errors
-                                       errors[address] = err?.message
+               if ('errors' in body && body.errors != null && typeof body.errors === 'object') {
+                       const errors = response.errors ?? Object.create(null)
+                       response.errors = errors
+                       for (const [address, error] of Object.entries(body.errors) as [string, unknown][]) {
+                               if (Account.isValid(address) && typeof error === 'string') {
+                                       errors[address] = error
                                }
                        }
                }
-       }
 
-       if (errors != null) {
-               if (typeof errors === 'object') {
-                       for (const [address, error] of Object.entries(errors) as [string, unknown][]) {
-                               try {
-                                       if (!Account.isValid(address)) {
-                                               throw new RpcError('Invalid account address')
-                                       }
-                                       if (typeof error === 'string') {
-                                               throw new Error(error)
-                                       }
-                               } catch (err: any) {
-                                       const errors = response.errors ?? Object.create(null)
-                                       response.errors = errors
-                                       errors[address] = err?.message
+               if ('frontiers' in body && body.frontiers != null && typeof body.frontiers === 'object') {
+                       const frontiers = response.frontiers ?? Object.create(null)
+                       response.frontiers = frontiers
+                       for (const [address, frontier] of Object.entries(frontiers) as [string, unknown][]) {
+                               if (Account.isValid(address) && hex.is(frontier, 64)) {
+                                       frontiers[address] = frontier
                                }
                        }
                }
-       }
 
-       return response
+               return response
+       }
+       throw new RpcError('Invalid accounts_balance response')
 }
index 2d44f903257815b38c0e15708489ec2b09a8811d..7f354b81a76e17879325de829b0cbf10971e840c 100644 (file)
@@ -20,35 +20,21 @@ export function blocks_info (body: unknown): BlocksInfoResponse {
                const response: BlocksInfoResponse = Object.create(null)
 
                if ('errors' in body && body.errors != null && typeof body.errors === 'object') {
+                       const errors = response.errors ?? Object.create(null)
+                       response.errors = errors
                        for (const [hash, error] of Object.entries(body.errors) as [string, unknown][]) {
-                               try {
-                                       if (hex.is(hash) && typeof error === 'string') {
-                                               throw new Error(error)
-                                       } else {
-                                               throw new Error('Invalid blocks_info error')
-                                       }
-                               } catch (err: any) {
-                                       const errors = response.errors ?? Object.create(null)
-                                       response.errors = errors
-                                       errors[hash] = err?.message
+                               if (hex.is(hash, 64) && typeof error === 'string') {
+                                       errors[hash] = error
                                }
                        }
                }
 
                if ('blocks' in body && body.blocks != null && typeof body.blocks === 'object') {
+                       const blocks: Record<Hex, BlockInfoResponse> = response.blocks ?? Object.create(null)
+                       response.blocks = blocks
                        for (const [hash, data] of Object.entries(body.blocks) as [string, unknown][]) {
-                               try {
-                                       if (hex.is(hash)) {
-                                               const blocks = response.blocks ?? Object.create(null)
-                                               response.blocks = blocks
-                                               blocks[hash] = block_info(data)
-                                       } else {
-                                               throw new Error('Invalid blocks_info hash')
-                                       }
-                               } catch (err: any) {
-                                       const errors = response.errors ?? Object.create(null)
-                                       response.errors = errors
-                                       errors[hash] = err?.message
+                               if (hex.is(hash, 64)) {
+                                       blocks[hash] = block_info(data)
                                }
                        }
                }