Skip to content

Commit ae9ce04

Browse files
authored
fix: improve faucet error handling (#3118)
* improve faucet erroring * update history * fix tests * fix history
1 parent 3f74511 commit ae9ce04

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

packages/xrpl/HISTORY.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ Subscribe to [the **xrpl-announce** mailing list](https://groups.google.com/g/xr
55
## Unreleased
66

77
### Fixed
8-
* Update ripple-binary-codec to 2.5.1 to address serialization/deserialization issues in `Issue` serialized type for MPTIssue.
8+
* Update ripple-binary-codec to 2.5.1 to address serialization/deserialization issues in `Issue` serialized type for `MPTIssue`.
9+
* Better faucet error handling
910

1011
## 4.4.3 (2025-11-07)
1112

packages/xrpl/src/Wallet/fundWallet.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,11 @@ export async function requestFunding(
153153
body: JSON.stringify(postBody),
154154
})
155155

156-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- it can be anything
157-
const body = await response.json()
158156
if (
159157
response.ok &&
160158
response.headers.get('Content-Type')?.startsWith('application/json')
161159
) {
160+
const body: unknown = await response.json()
162161
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- It's a FaucetWallet
163162
const classicAddress = (body as FaucetWallet).account.classicAddress
164163
return processSuccessfulResponse(
@@ -168,7 +167,7 @@ export async function requestFunding(
168167
startingBalance,
169168
)
170169
}
171-
return processError(response, body)
170+
return processError(response)
172171
}
173172

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

209-
async function processError(response: Response, body): Promise<never> {
208+
interface ErrorData {
209+
body?: unknown
210+
contentType?: string
211+
statusCode: number
212+
}
213+
214+
async function processError(response: Response): Promise<never> {
215+
const errorData: ErrorData = {
216+
contentType: response.headers.get('Content-Type') ?? undefined,
217+
statusCode: response.status,
218+
}
219+
const clone = response.clone()
220+
try {
221+
const body: unknown = await response.json()
222+
errorData.body = body
223+
} catch {
224+
errorData.body = await clone.text()
225+
}
210226
return Promise.reject(
211-
new XRPLFaucetError(
212-
`Request failed: ${JSON.stringify({
213-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- json response could be anything
214-
body: body ?? {},
215-
contentType: response.headers.get('Content-Type'),
216-
statusCode: response.status,
217-
})}`,
218-
),
227+
new XRPLFaucetError(`Request failed: ${JSON.stringify(errorData)}`),
219228
)
220229
}
221230

packages/xrpl/test/faucet/fundWallet.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,13 @@ describe('fundWallet', function () {
121121

122122
throw new Error('Error not thrown')
123123
} catch (error) {
124-
await api.disconnect()
125124
expect(error).toEqual(
126125
new XRPLFaucetError(
127-
'Request failed: {"body":{"error":"Invalid amount","detail":"Must be an integer"},"contentType":"application/json; charset=utf-8","statusCode":400}',
126+
'Request failed: {"contentType":"application/json; charset=utf-8","statusCode":400,"body":{"error":"Invalid amount","detail":"Must be an integer"}}',
128127
),
129128
)
129+
} finally {
130+
await api.disconnect()
130131
}
131132
})
132133
})

0 commit comments

Comments
 (0)