diff --git a/errors/src/errors/utils/util_errors.rs b/errors/src/errors/utils/util_errors.rs index e81a26c5fd..08a02995ea 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: (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 2120debc0e..539fdd7062 100644 --- a/leo/cli/commands/mod.rs +++ b/leo/cli/commands/mod.rs @@ -282,40 +282,51 @@ 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::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 { @@ -337,5 +348,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..465d419375 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,14 +519,17 @@ 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()))?; - 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())), } }