Skip to content

Commit 5db69c4

Browse files
committed
fix(rpc): add try-catch error handling to fundAddress()
Wrap HTTP request in try-catch to provide cleaner error messages when friendbot returns errors (e.g., when funding an already-funded address).
1 parent 8ce245e commit 5db69c4

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

src/rpc/server.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,7 +1273,7 @@ export class RpcServer {
12731273
* @param {string} address The address to fund. Can be either a Stellar
12741274
* account (G...) or contract (C...) address.
12751275
* @param {string} [friendbotUrl] Optionally, an explicit Friendbot URL
1276-
* (by default: this calls the Soroban RPC
1276+
* (by default: this calls the Stellar RPC
12771277
* {@link module:rpc.Server#getNetwork | getNetwork} method to try to
12781278
* discover this network's Friendbot url).
12791279
* @returns {Promise<Api.GetSuccessfulTransactionResponse>} The transaction
@@ -1313,18 +1313,25 @@ export class RpcServer {
13131313
throw new Error("No friendbot URL configured for current network");
13141314
}
13151315

1316-
const response = await this.httpClient.post<FriendbotApi.Response>(
1317-
`${friendbotUrl}?addr=${encodeURIComponent(address)}`,
1318-
);
1319-
1320-
const txResponse = await this.getTransaction(response.data.hash);
1321-
if (txResponse.status !== Api.GetTransactionStatus.SUCCESS) {
1322-
throw new Error(
1323-
`Funding address ${address} failed: transaction status ${txResponse.status}`,
1316+
try {
1317+
const response = await this.httpClient.post<FriendbotApi.Response>(
1318+
`${friendbotUrl}?addr=${encodeURIComponent(address)}`,
13241319
);
1325-
}
13261320

1327-
return txResponse;
1321+
const txResponse = await this.getTransaction(response.data.hash);
1322+
if (txResponse.status !== Api.GetTransactionStatus.SUCCESS) {
1323+
throw new Error(
1324+
`Funding address ${address} failed: transaction status ${txResponse.status}`,
1325+
);
1326+
}
1327+
1328+
return txResponse;
1329+
} catch (error: any) {
1330+
if (error.response?.status === 400) {
1331+
throw new Error(error.response.data?.detail ?? "Bad Request");
1332+
}
1333+
throw error;
1334+
}
13281335
}
13291336

13301337
/**

0 commit comments

Comments
 (0)