Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/xrpl/HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Subscribe to [the **xrpl-announce** mailing list](https://groups.google.com/g/xr

### Fixed
* Fix incorrect type checking in `validateVaultCreate` that prevented vault creation with MPT as an asset.
* Better faucet error handling

## 4.4.2 (2025-09-25)

Expand Down
33 changes: 21 additions & 12 deletions packages/xrpl/src/Wallet/fundWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,11 @@ export async function requestFunding(
body: JSON.stringify(postBody),
})

// 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 body: unknown = await response.json()
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- It's a FaucetWallet
const classicAddress = (body as FaucetWallet).account.classicAddress
return processSuccessfulResponse(
Expand All @@ -168,7 +167,7 @@ export async function requestFunding(
startingBalance,
)
}
return processError(response, body)
return processError(response)
}

// eslint-disable-next-line max-params -- Only used as a helper function, lines inc due to added balance.
Expand Down Expand Up @@ -206,16 +205,26 @@ async function processSuccessfulResponse(
)
}

async function processError(response: Response, body): Promise<never> {
interface ErrorData {
body?: unknown
contentType?: string
statusCode: number
}

async function processError(response: Response): Promise<never> {
const errorData: ErrorData = {
contentType: response.headers.get('Content-Type') ?? undefined,
Copy link

Copilot AI Oct 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ?? undefined is redundant since response.headers.get('Content-Type') already returns string | null. When assigned to an optional property (contentType?: string), null will be automatically converted to undefined without the explicit coalescing operator.

Suggested change
contentType: response.headers.get('Content-Type') ?? undefined,
contentType: response.headers.get('Content-Type'),

Copilot uses AI. Check for mistakes.
statusCode: response.status,
}
const clone = response.clone()
try {
const body: unknown = await response.json()
errorData.body = body
} catch {
errorData.body = await clone.text()
}
return Promise.reject(
new XRPLFaucetError(
`Request failed: ${JSON.stringify({
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- json response could be anything
body: body ?? {},
contentType: response.headers.get('Content-Type'),
statusCode: response.status,
})}`,
),
new XRPLFaucetError(`Request failed: ${JSON.stringify(errorData)}`),
)
}

Expand Down
5 changes: 3 additions & 2 deletions packages/xrpl/test/faucet/fundWallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,13 @@ describe('fundWallet', function () {

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}',
'Request failed: {"contentType":"application/json; charset=utf-8","statusCode":400,"body":{"error":"Invalid amount","detail":"Must be an integer"}}',
),
)
} finally {
await api.disconnect()
}
})
})
Loading