From e9e2926d5f22b2a673db34eeaaec08fbd24d7fd6 Mon Sep 17 00:00:00 2001 From: Pranav Gaddamadugu <23022326+d0cd@users.noreply.github.com> Date: Fri, 13 Sep 2024 19:18:05 -0700 Subject: [PATCH 1/3] Update error messaging to account for redirects from APIs --- errors/src/errors/utils/util_errors.rs | 7 +++++ leo/cli/commands/mod.rs | 37 +++++++++++++++----------- utils/retriever/src/retriever/mod.rs | 8 +++--- 3 files changed, 33 insertions(+), 19 deletions(-) diff --git a/errors/src/errors/utils/util_errors.rs b/errors/src/errors/utils/util_errors.rs index e81a26c5fd..6fac900240 100644 --- a/errors/src/errors/utils/util_errors.rs +++ b/errors/src/errors/utils/util_errors.rs @@ -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: (), + msg: format!("The endpoint has been permanently moved to a different location."), + help: Some("Try using `https://api.explorer.provable.com/v1` instead.".to_string()), + } ); diff --git a/leo/cli/commands/mod.rs b/leo/cli/commands/mod.rs index 2120debc0e..e6df73a292 100644 --- a/leo/cli/commands/mod.rs +++ b/leo/cli/commands/mod.rs @@ -282,40 +282,47 @@ fn handle_broadcast(endpoint: &String, transaction: Transaction, // 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::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 = + "⚠️ The endpoint has been permanently moved. Try using `https://api.explorer.provable.com/v1` instead."; + 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 { @@ -337,5 +344,5 @@ fn handle_broadcast(endpoint: &String, transaction: Transaction, Err(CliError::broadcast_error(msg).into()) } - }; + } } diff --git a/utils/retriever/src/retriever/mod.rs b/utils/retriever/src/retriever/mod.rs index 0410eb1186..7f05d6c99e 100644 --- a/utils/retriever/src/retriever/mod.rs +++ b/utils/retriever/src/retriever/mod.rs @@ -522,10 +522,10 @@ pub fn fetch_from_network(url: &str) -> Result { .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()), + _ => Err(UtilError::network_error(url, response.status(), Default::default())), } } From 9fc07a7704247e1fa186628573002c81d7d7a4ff Mon Sep 17 00:00:00 2001 From: Pranav Gaddamadugu <23022326+d0cd@users.noreply.github.com> Date: Fri, 13 Sep 2024 20:15:51 -0700 Subject: [PATCH 2/3] Dont automatically redirect --- leo/cli/commands/mod.rs | 5 ++++- utils/retriever/src/retriever/mod.rs | 6 +++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/leo/cli/commands/mod.rs b/leo/cli/commands/mod.rs index e6df73a292..4fe9c7d742 100644 --- a/leo/cli/commands/mod.rs +++ b/leo/cli/commands/mod.rs @@ -283,7 +283,10 @@ fn handle_broadcast(endpoint: &String, transaction: Transaction, let transaction_id = transaction.id(); // Send the deployment request to the endpoint. - let response = ureq::post(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()))?; diff --git a/utils/retriever/src/retriever/mod.rs b/utils/retriever/src/retriever/mod.rs index 7f05d6c99e..7e17c289df 100644 --- a/utils/retriever/src/retriever/mod.rs +++ b/utils/retriever/src/retriever/mod.rs @@ -33,6 +33,7 @@ use std::{ path::{Path, PathBuf}, str::FromStr, }; +use ureq::AgentBuilder; // Retriever is responsible for retrieving external programs pub struct Retriever { @@ -518,7 +519,10 @@ fn retrieve_from_network( // Fetch the given endpoint url and return the sanitized response. pub fn fetch_from_network(url: &str) -> Result { - 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()))?; From f9bd1d260f1091ec4e441caa2693afda32c97ef9 Mon Sep 17 00:00:00 2001 From: Pranav Gaddamadugu <23022326+d0cd@users.noreply.github.com> Date: Mon, 16 Sep 2024 15:12:51 +0800 Subject: [PATCH 3/3] Minor nit --- errors/src/errors/utils/util_errors.rs | 6 +++--- leo/cli/commands/mod.rs | 5 +++-- utils/retriever/src/retriever/mod.rs | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/errors/src/errors/utils/util_errors.rs b/errors/src/errors/utils/util_errors.rs index 6fac900240..08a02995ea 100644 --- a/errors/src/errors/utils/util_errors.rs +++ b/errors/src/errors/utils/util_errors.rs @@ -203,8 +203,8 @@ create_messages!( @backtraced endpoint_moved_error { - args: (), - msg: format!("The endpoint has been permanently moved to a different location."), - help: Some("Try using `https://api.explorer.provable.com/v1` instead.".to_string()), + 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()), } ); diff --git a/leo/cli/commands/mod.rs b/leo/cli/commands/mod.rs index 4fe9c7d742..539fdd7062 100644 --- a/leo/cli/commands/mod.rs +++ b/leo/cli/commands/mod.rs @@ -317,8 +317,9 @@ fn handle_broadcast(endpoint: &String, transaction: Transaction, Ok(()) } 301 => { - let msg = - "⚠️ The endpoint has been permanently moved. Try using `https://api.explorer.provable.com/v1` instead."; + 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()) } _ => { diff --git a/utils/retriever/src/retriever/mod.rs b/utils/retriever/src/retriever/mod.rs index 7e17c289df..465d419375 100644 --- a/utils/retriever/src/retriever/mod.rs +++ b/utils/retriever/src/retriever/mod.rs @@ -528,7 +528,7 @@ pub fn fetch_from_network(url: &str) -> Result { .map_err(|err| UtilError::failed_to_retrieve_from_endpoint(err, Default::default()))?; match response.status() { 200 => Ok(response.into_string().unwrap().replace("\\n", "\n").replace('\"', "")), - 301 => Err(UtilError::endpoint_moved_error()), + 301 => Err(UtilError::endpoint_moved_error(url)), _ => Err(UtilError::network_error(url, response.status(), Default::default())), } }