fix: faucet errors not showing returned body (#2452)

fetch only lets you read the body once so we need to save the variable

Before it would throw TypeError: body used already for: https://faucet.devnet.rippletest.net/accounts
This commit is contained in:
Caleb Kniffen
2023-11-02 10:23:55 -05:00
parent 83b9780b5b
commit 88d8a7b73e
2 changed files with 30 additions and 7 deletions

View File

@@ -158,14 +158,14 @@ export async function requestFunding(
body: JSON.stringify(postBody),
})
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- It's a FaucetWallet
const body = (await response.json()) as FaucetWallet
// "application/json; charset=utf-8"
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- it can be anything
const body = await response.json()
if (
response.ok &&
response.headers.get('Content-Type')?.startsWith('application/json')
) {
const classicAddress = body.account.classicAddress
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- It's a FaucetWallet
const classicAddress = (body as FaucetWallet).account.classicAddress
return processSuccessfulResponse(
client,
classicAddress,
@@ -173,7 +173,7 @@ export async function requestFunding(
startingBalance,
)
}
return processError(response)
return processError(response, body)
}
// eslint-disable-next-line max-params -- Only used as a helper function, lines inc due to added balance.
@@ -218,12 +218,12 @@ async function processSuccessfulResponse(
}
}
async function processError(response: Response): Promise<never> {
async function processError(response: Response, body): Promise<never> {
return Promise.reject(
new XRPLFaucetError(
`Request failed: ${JSON.stringify({
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- json response could be anything
body: (await response.json()) || {},
body: body || {},
contentType: response.headers.get('Content-Type'),
statusCode: response.status,
})}`,

View File

@@ -5,6 +5,7 @@ import {
isValidClassicAddress,
isValidXAddress,
dropsToXrp,
XRPLFaucetError,
} from '../../src'
async function generate_faucet_wallet_and_fund_again(
@@ -140,4 +141,26 @@ describe('fundWallet', function () {
},
TIMEOUT,
)
it('handles errors', async () => {
const api = new Client('wss://s.altnet.rippletest.net:51233')
await api.connect()
// jasmine and jest handle async differently so need to use try catch approach instead of `expect.rejects` or `expectAsync`
try {
await api.fundWallet(null, {
amount: '-1000',
usageContext: 'integration-test',
})
throw new Error('Error not thrown')
} catch (error) {
await api.disconnect()
expect(error).toEqual(
new XRPLFaucetError(
'Request failed: {"body":{"error":"Invalid amount","detail":"Must be an integer"},"contentType":"application/json; charset=utf-8","statusCode":400}',
),
)
}
})
})