Skip to content

Commit

Permalink
Merge pull request #28349 from ProvableHQ/update/api-endpoint
Browse files Browse the repository at this point in the history
Update error messaging to account for redirects from APIs
  • Loading branch information
d0cd authored Sep 17, 2024
2 parents 91d4542 + f9bd1d2 commit 9a0d9e5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 20 deletions.
7 changes: 7 additions & 0 deletions errors/src/errors/utils/util_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,11 @@ create_messages!(
msg: format!("Invalid bound: {bound}."),
help: Some("Bound must be a valid u32.".to_string()),
}

@backtraced
endpoint_moved_error {
args: (endpoint: impl Display),
msg: format!("The endpoint `{endpoint}` has been permanently moved."),
help: Some("Try using `https://api.explorer.provable.com/v1` in your `.env` file or via the `--endpoint` flag.".to_string()),
}
);
41 changes: 26 additions & 15 deletions leo/cli/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,40 +282,51 @@ fn handle_broadcast<N: Network>(endpoint: &String, transaction: Transaction<N>,
// Get the transaction id.
let transaction_id = transaction.id();

// Send the deployment request to the local development node.
return match ureq::post(endpoint).send_json(&transaction) {
Ok(id) => {
// Send the deployment request to the endpoint.
let response = ureq::AgentBuilder::new()
.redirects(0)
.build()
.post(endpoint)
.set(&format!("X-Aleo-Leo-{}", env!("CARGO_PKG_VERSION")), "true")
.send_json(&transaction)
.map_err(|err| CliError::broadcast_error(err.to_string()))?;
match response.status() {
200 => {
// Remove the quotes from the response.
let _response_string =
id.into_string().map_err(CliError::string_parse_error)?.trim_matches('\"').to_string();

response.into_string().map_err(CliError::string_parse_error)?.trim_matches('\"').to_string();
match transaction {
Transaction::Deploy(..) => {
println!(
"⌛ Deployment {transaction_id} ('{}') has been broadcast to {}.\n",
operation.bold(),
endpoint
)
);
}
Transaction::Execute(..) => {
println!(
"⌛ Execution {transaction_id} ('{}') has been broadcast to {}.\n",
operation.bold(),
endpoint
)
);
}
Transaction::Fee(..) => {
println!("❌ Failed to broadcast fee '{}' to the {}.\n", operation.bold(), endpoint)
println!("❌ Failed to broadcast fee '{}' to the {}.\n", operation.bold(), endpoint);
}
}
Ok(())
}
Err(error) => {
let error_message = match error {
ureq::Error::Status(code, response) => {
format!("(status code {code}: {:?})", response.into_string().map_err(CliError::string_parse_error)?)
}
ureq::Error::Transport(err) => format!("({err})"),
301 => {
let msg = format!(
"⚠️ The endpoint `{endpoint}` has been permanently moved. Try using `https://api.explorer.provable.com/v1` in your `.env` file or via the `--endpoint` flag."
);
Err(CliError::broadcast_error(msg).into())
}
_ => {
let code = response.status();
let error_message = match response.into_string() {
Ok(response) => format!("(status code {code}: {:?})", response),
Err(err) => format!("({err})"),
};

let msg = match transaction {
Expand All @@ -337,5 +348,5 @@ fn handle_broadcast<N: Network>(endpoint: &String, transaction: Transaction<N>,

Err(CliError::broadcast_error(msg).into())
}
};
}
}
14 changes: 9 additions & 5 deletions utils/retriever/src/retriever/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use std::{
path::{Path, PathBuf},
str::FromStr,
};
use ureq::AgentBuilder;

// Retriever is responsible for retrieving external programs
pub struct Retriever<N: Network> {
Expand Down Expand Up @@ -518,14 +519,17 @@ fn retrieve_from_network<N: Network>(

// Fetch the given endpoint url and return the sanitized response.
pub fn fetch_from_network(url: &str) -> Result<String, UtilError> {
let response = ureq::get(url)
let response = AgentBuilder::new()
.redirects(0)
.build()
.get(url)
.set(&format!("X-Aleo-Leo-{}", env!("CARGO_PKG_VERSION")), "true")
.call()
.map_err(|err| UtilError::failed_to_retrieve_from_endpoint(err, Default::default()))?;
if response.status() == 200 {
Ok(response.into_string().unwrap().replace("\\n", "\n").replace('\"', ""))
} else {
Err(UtilError::network_error(url, response.status(), Default::default()))
match response.status() {
200 => Ok(response.into_string().unwrap().replace("\\n", "\n").replace('\"', "")),
301 => Err(UtilError::endpoint_moved_error(url)),
_ => Err(UtilError::network_error(url, response.status(), Default::default())),
}
}

Expand Down

0 comments on commit 9a0d9e5

Please sign in to comment.