Skip to content

Commit

Permalink
fix: Gracefully handle account creation request errors from a faucet …
Browse files Browse the repository at this point in the history
…service [testnet-only] (#366)
  • Loading branch information
AlexKushnir1 authored Aug 7, 2024
1 parent 6502c12 commit f24c662
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion workspaces/src/rpc/tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub(crate) async fn url_create_account(
let helper_url = helper_url.join("account").unwrap();

// TODO(maybe): need this in near-jsonrpc-client as well:
let _resp = reqwest::Client::new()
let response = reqwest::Client::new()
.post(helper_url)
.header("Content-Type", "application/json")
.body(
Expand All @@ -57,6 +57,40 @@ pub(crate) async fn url_create_account(
.await
.map_err(|e| RpcErrorCode::HelperAccountCreationFailure.custom(e))?;

if response.status() >= reqwest::StatusCode::BAD_REQUEST {
return Err(ErrorKind::Other.message(format!(
"The faucet (helper service) server failed with status code <{}>",
response.status()
)));
}

let account_creation_transaction = response
.json::<near_jsonrpc_client::methods::tx::RpcTransactionResponse>()
.await
.map_err(|err| ErrorKind::DataConversion.custom(err))?
.final_execution_outcome
.map(|outcome| outcome.into_outcome())
.ok_or_else(|| {
ErrorKind::Other.message(
"The faucet (helper service) server did not return a transaction response.",
)
})?;

match account_creation_transaction.status {
near_primitives::views::FinalExecutionStatus::SuccessValue(ref value) => {
if value == b"false" {
return Err(ErrorKind::Other.message(format!(
"The new account <{}> could not be created successfully.",
&account_id
)));
}
}
near_primitives::views::FinalExecutionStatus::Failure(err) => {
return Err(ErrorKind::Execution.custom(err));
}
_ => unreachable!(),
}

Ok(())
}

Expand Down

0 comments on commit f24c662

Please sign in to comment.