From a7af6f5c858018aaa8da69e910ccae80a4250496 Mon Sep 17 00:00:00 2001 From: Martin Wimpress Date: Sun, 25 May 2025 13:15:19 +0100 Subject: [PATCH] refactor(main): centralise exit logic by returning Result Removes direct calls to `std::process::exit` from `fetch_xml_metadata` and error handling blocks within `main`. These functions now propagate errors by returning `Result`. This change improves testability and aligns with idiomatic Rust error handling, allowing `main` to be the single point of exit for the application on error. Spinner messages for context are retained. --- src/main.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index 6d8f238..6829f5e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,7 +13,6 @@ use ia_get::archive_metadata::{XmlFiles, parse_xml_files}; use indicatif::ProgressStyle; use reqwest::Client; use clap::Parser; -use std::process; /// Checks if a URL is accessible by sending a HEAD request async fn is_url_accessible(url: &str, client: &Client) -> Result<()> { @@ -74,8 +73,7 @@ async fn fetch_xml_metadata( // Check XML URL accessibility if let Err(e) = is_url_accessible(&xml_url, client).await { spinner.finish_with_message(format!("🔴 XML metadata not accessible: {}", xml_url)); - eprintln!("╰╼ Exiting due to error: {}", e); - process::exit(1); + return Err(e); // Propagate the error } spinner.set_message("Parsing archive metadata... 👀"); @@ -130,8 +128,7 @@ async fn main() -> std::result::Result<(), Box> { // Check URL accessibility if let Err(e) = is_url_accessible(&cli.url, &client).await { spinner.finish_with_message(format!("🔴 Archive.org URL not accessible: {}", cli.url)); - eprintln!("╰╼ Exiting due to error: {}", e); - process::exit(1); + return Err(e.into()); // Propagate error } // Fetch and parse XML metadata in one operation