diff --git a/AGENTS.md b/AGENTS.md index 723be8b..3fadb87 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -7,7 +7,7 @@ This repository contains a synchronous Rust utility that fetches product data fr 2. **Build/Run Order:** The intended command sequence is: `cargo build --release` followed by running the binary (`./target/release/antares_get_data`). 3. **Development Flow:** Use this order when developing or verifying changes: * `cargo fmt --all -- --check` (Linting) - - `cargo check` (Compile/Verify) + * `cargo check` (Compile/Verify) - run after any file modifications * `cargo test` (Testing) ## 💻 Architecture & Quirks diff --git a/src/main.rs b/src/main.rs index 9f0bbe7..80019c6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,11 +11,29 @@ use api::make_url; use template::antares::Antares; use tools::{Logger, export_to_excel}; -fn make_request(url: &str) -> Result> { +fn make_request(url: &str, logger: &Logger) -> Result> { let client = Client::builder() .timeout(Duration::from_secs(300)) - .build()?; - Ok(client.get(url).send()?.text()?) + .build().map_err(|e| { + logger.log_error(&format!("Failed to create HTTP client: {}", e)); + Box::new(std::io::Error::new(std::io::ErrorKind::Other, e.to_string())) + })?; + + let response = client.get(url).send().map_err(|e| { + logger.log_error(&format!("HTTP request failed: {}", e)); + Box::new(std::io::Error::new(std::io::ErrorKind::ConnectionRefused, e.to_string())) + })?; + + if response.status().is_success() { + Ok(response.text().map_err(|e| { + logger.log_error(&format!("Failed to read response body: {}", e)); + Box::new(std::io::Error::new(std::io::ErrorKind::Other, e.to_string())) + })?) + } else { + let error_msg = format!("HTTP error: {:?}", response.status()); + logger.log_error(&error_msg); + Err(Box::new(std::io::Error::new(std::io::ErrorKind::ConnectionRefused, error_msg))) + } } fn main() -> Result<(), Box> { @@ -37,13 +55,12 @@ fn main() -> Result<(), Box> { let url = make_url(base, &usercode, &password, cikkszam); - let response = match make_request(&url) { + let response = match make_request(&url, &logger) { Ok(resp) => { logger.log_info("API request sent successfully"); resp } Err(e) => { - logger.log_api_failure(&e.to_string()); return Err(e); } }; @@ -52,16 +69,34 @@ fn main() -> Result<(), Box> { let items: Antares = match serde_json::from_str::(&response) { Ok(items) => { // Re-serialize via the structs to ensure consistent formatting - let pretty = serde_json::to_string_pretty(&items)?; - fs::write("antares.json", pretty)?; + let pretty = match serde_json::to_string_pretty(&items) { + Ok(pretty) => pretty, + Err(e) => { + logger.log_error(&format!("Failed to serialize items: {}", e)); + return Err(Box::new(e)); + } + }; + fs::write("antares.json", &pretty).map_err(|e| { + logger.log_error(&format!("Failed to write antares.json: {}", e)); + Box::new(std::io::Error::new(std::io::ErrorKind::Other, e.to_string())) + })?; logger.log_api_success(items.len()); items } Err(_) => { // Fall back to pretty-printing raw JSON value, or raw text. if let Ok(json_val) = serde_json::from_str::(&response) { - let pretty = serde_json::to_string_pretty(&json_val)?; - fs::write("antares.json", pretty)?; + let pretty = match serde_json::to_string_pretty(&json_val) { + Ok(pretty) => pretty, + Err(e) => { + logger.log_error(&format!("Failed to serialize JSON: {}", e)); + return Err(Box::new(e)); + } + }; + fs::write("antares.json", &pretty).map_err(|e| { + logger.log_error(&format!("Failed to write antares.json: {}", e)); + Box::new(std::io::Error::new(std::io::ErrorKind::Other, e.to_string())) + })?; logger.log_info("Response saved as JSON (schema mismatch)"); } else { fs::write("antares.json", &response)?; diff --git a/src/tools/logger.rs b/src/tools/logger.rs index 9dedf92..503d529 100644 --- a/src/tools/logger.rs +++ b/src/tools/logger.rs @@ -40,10 +40,6 @@ impl Logger { )); } - pub fn log_api_failure(&self, error: &str) { - self.log(&format!("✗ API call failed: {}", error)); - } - pub fn log_export_success(&self, filepath: &str, row_count: usize) { self.log(&format!( "✓ Export successful - {} rows exported to {}", @@ -58,4 +54,8 @@ impl Logger { pub fn log_info(&self, message: &str) { self.log(&format!("ℹ {}", message)); } + + pub fn log_error(&self, error: &str) { + self.log(&format!("✗ Error: {}", error)); + } }