From f673f2d28052ee6f105d8d69efe8569621bc149a Mon Sep 17 00:00:00 2001 From: ignaciopenia <108410122+ignaciopenia@users.noreply.github.com> Date: Wed, 24 Jul 2024 00:50:15 -0300 Subject: [PATCH] Fix/handle other errors (#13) * add tests to mock-externals suite * tests: complete custom and alternative routes coverage * handle STACKS_API_HOST error * tests: lint fix * tests: empty route coverage * refactor mock-externals suite * tests: coverage on fungible token balances and null token cases * refactor mock-externals suite * tests: remove unused structure * temp commit * remove redundant describe * lint fix --------- Co-authored-by: simsbluebox Co-authored-by: david weil --- src/utils/fetchData.ts | 12 ++++++++---- test/alexSDK.mock-externals.test.ts | 6 +++--- test/alexSDK.test.ts | 2 +- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/utils/fetchData.ts b/src/utils/fetchData.ts index 920e441..59c9709 100644 --- a/src/utils/fetchData.ts +++ b/src/utils/fetchData.ts @@ -64,9 +64,13 @@ export async function fetchBalanceForAccount( stxAddress: string, tokenMappings: TokenInfo[] ): Promise> { - const response: AddressBalanceResponse = await fetch( + const response = await fetch( `${configs.STACKS_API_HOST}/extended/v1/address/${stxAddress}/balances` - ).then((a) => a.json()); + ); + if (!response.ok) { + throw new Error('Failed to fetch account balances'); + } + const balanceData: AddressBalanceResponse = await response.json(); return fromEntries( await Promise.all( tokenMappings.map(async (a) => { @@ -95,10 +99,10 @@ export async function fetchBalanceForAccount( ]; } if (a.id === Currency.STX) { - return [a.id, BigInt(response.stx.balance) * BigInt(100)]; + return [a.id, BigInt(balanceData.stx.balance) * BigInt(100)]; } const fungibleToken = - response.fungible_tokens[a.underlyingToken]?.balance; + balanceData.fungible_tokens[a.underlyingToken]?.balance; if (fungibleToken == null) { return [a.id, BigInt(0)]; } diff --git a/test/alexSDK.mock-externals.test.ts b/test/alexSDK.mock-externals.test.ts index c549bfc..98d4de1 100644 --- a/test/alexSDK.mock-externals.test.ts +++ b/test/alexSDK.mock-externals.test.ts @@ -104,7 +104,7 @@ describe('AlexSDK - mock externals - SDK_API_HOST - BACKEND_API_HOST - STACKS_AP it('Attempt to Get Balances with incorrect data', async () => { await expect( fetchBalanceForAccount(stxAddress, tokenMappings) - ).rejects.toThrow('Unexpected'); + ).rejects.toThrow('Failed to fetch account balances'); }); }); describe('AlexSDK - mock externals - SDK_API_HOST - BACKEND_API_HOST - STACKS_API_HOST (Gateway Timeout)', () => { @@ -187,7 +187,7 @@ describe('AlexSDK - mock externals - SDK_API_HOST - BACKEND_API_HOST - STACKS_AP it('Attempt to Get Balances with incorrect data', async () => { await expect( fetchBalanceForAccount(stxAddress, tokenMappings) - ).rejects.toThrow('Unexpected'); + ).rejects.toThrow('Failed to fetch account balances'); }); }); describe('AlexSDK - mock externals - SDK_API_HOST - BACKEND_API_HOST - STACKS_API_HOST (Not Found)', () => { @@ -270,7 +270,7 @@ describe('AlexSDK - mock externals - SDK_API_HOST - BACKEND_API_HOST - STACKS_AP it('Attempt to Get Balances with incorrect data', async () => { await expect( fetchBalanceForAccount(stxAddress, tokenMappings) - ).rejects.toThrow('Unexpected'); + ).rejects.toThrow('Failed to fetch account balances'); }); }); describe('Transfer Factory', () => { diff --git a/test/alexSDK.test.ts b/test/alexSDK.test.ts index d17cdfb..aa19546 100644 --- a/test/alexSDK.test.ts +++ b/test/alexSDK.test.ts @@ -297,7 +297,7 @@ describe('AlexSDK', () => { // TODO: Implement principal address verification in the SDK methods. const wrongAddress = 'ABC'; await expect(sdk.getBalances(wrongAddress)).rejects.toThrow( - "Cannot read properties of undefined (reading 'balance')" + 'Failed to fetch account balances' ); }, 10000);