Skip to content

Commit

Permalink
Update fund error handling (#1501)
Browse files Browse the repository at this point in the history
* Update fund error handling

* fix

* fix

* add test

* fix

* fix
  • Loading branch information
leighmcculloch authored Jul 30, 2024
1 parent 4a7349f commit ba24fda
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 13 deletions.
1 change: 1 addition & 0 deletions cmd/crates/soroban-test/tests/it/integration.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod bindings;
mod custom_types;
mod dotenv;
mod fund;
mod hello_world;
mod tx;
mod util;
Expand Down
19 changes: 19 additions & 0 deletions cmd/crates/soroban-test/tests/it/integration/fund.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use soroban_test::TestEnv;

#[tokio::test]
#[allow(clippy::too_many_lines)]
async fn fund() {
let sandbox = &TestEnv::new();
sandbox
.new_assert_cmd("keys")
.arg("generate")
.arg("test")
.assert()
.success();
sandbox
.new_assert_cmd("keys")
.arg("fund")
.arg("test")
.assert()
.stderr(predicates::str::contains("funding failed"));
}
6 changes: 0 additions & 6 deletions cmd/crates/soroban-test/tests/it/integration/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@ async fn invoke() {
let sandbox = &TestEnv::new();
let c = soroban_rpc::Client::new(&sandbox.rpc_url).unwrap();
let GetLatestLedgerResponse { sequence, .. } = c.get_latest_ledger().await.unwrap();
sandbox
.new_assert_cmd("keys")
.arg("fund")
.arg("test")
.assert()
.stderr(predicates::str::contains("Account already exists"));
sandbox
.new_assert_cmd("keys")
.arg("fund")
Expand Down
14 changes: 7 additions & 7 deletions cmd/soroban-cli/src/config/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ pub enum Error {
FailedToParseJSON(String, serde_json::Error),
#[error("Invalid URL {0}")]
InvalidUrl(String),
#[error("Inproper response {0}")]
InproperResponse(String),
#[error("funding failed: {0}")]
FundingFailed(String),
}

#[derive(Debug, clap::Args, Clone, Default)]
Expand Down Expand Up @@ -149,16 +149,16 @@ impl Network {
return Err(Error::InvalidUrl(uri.to_string()));
}
};
let request_successful = response.status().is_success();
let body = hyper::body::to_bytes(response.into_body()).await?;
let res = serde_json::from_slice::<serde_json::Value>(&body)
.map_err(|e| Error::FailedToParseJSON(uri.to_string(), e))?;
tracing::debug!("{res:#?}");
if let Some(detail) = res.get("detail").and_then(Value::as_str) {
if detail.contains("createAccountAlreadyExist") {
eprintln!("Account already exists");
if !request_successful {
if let Some(detail) = res.get("detail").and_then(Value::as_str) {
return Err(Error::FundingFailed(detail.to_string()));
}
} else if res.get("successful").is_none() {
return Err(Error::InproperResponse(res.to_string()));
return Err(Error::FundingFailed("unknown cause".to_string()));
}
Ok(())
}
Expand Down

0 comments on commit ba24fda

Please sign in to comment.