From dc1b2bde27c166090fa43067063d761f791c81f8 Mon Sep 17 00:00:00 2001 From: Andrew Scott <91222421+andcscott@users.noreply.github.com> Date: Fri, 18 Oct 2024 16:36:15 +0000 Subject: [PATCH] feat: add `source` to `VersionManagerError` errors (#32) Replaced technical errors from the machine with the attribute source from the error crate. --- src/error.rs | 37 ++++++++++++++----- src/version_manager/node_version_manager.rs | 14 +++---- src/version_manager/python_version_manager.rs | 22 +++++------ 3 files changed, 45 insertions(+), 28 deletions(-) diff --git a/src/error.rs b/src/error.rs index 4f9f802..1a2dbbc 100644 --- a/src/error.rs +++ b/src/error.rs @@ -17,8 +17,12 @@ pub enum VersionManagerError { #[error("Unable to find home directory")] UnableHomeDirectory {}, - #[error("Failed to download version from '{url}' | {error}")] - DownloadError { url: String, error: reqwest::Error }, + #[error("Failed to download version from '{url}' | {source}")] + DownloadError { + url: String, + #[source] + source: reqwest::Error, + }, #[error("Failed to download {package} from '{url}' | Status: {status}")] FailedDownloadPackage { @@ -27,14 +31,26 @@ pub enum VersionManagerError { status: String, }, - #[error("Failed to create download file '{file}' | {error}")] - FailedCreateFile { file: String, error: std::io::Error }, + #[error("Failed to create download file '{file}' | {source}")] + FailedCreateFile { + file: String, + #[source] + source: std::io::Error, + }, - #[error("Failed to write a compressed file '{file}' | {error}")] - FailedWriteFile { file: String, error: reqwest::Error }, + #[error("Failed to write a compressed file '{file}' | {source}")] + FailedWriteFile { + file: String, + #[source] + source: reqwest::Error, + }, - #[error("Failed to remove downloaded archive '{file}' | {error}")] - FailedDeleteFile { file: String, error: std::io::Error }, + #[error("Failed to remove downloaded archive '{file}' | {source}")] + FailedDeleteFile { + file: String, + #[source] + source: std::io::Error, + }, #[error("Failed to extract archive '{file}' to '{target}' | {error}")] FailedExtractArchive { @@ -43,10 +59,11 @@ pub enum VersionManagerError { error: String, }, - #[error("Failed to run command '{command}' | {error}")] + #[error("Failed to run command '{command}' | {source}")] FailedRunCommand { command: String, - error: std::io::Error, + #[source] + source: std::io::Error, }, #[error("{package} build command failed | Exit code: {status}{error}")] diff --git a/src/version_manager/node_version_manager.rs b/src/version_manager/node_version_manager.rs index caec180..7abe2e2 100644 --- a/src/version_manager/node_version_manager.rs +++ b/src/version_manager/node_version_manager.rs @@ -99,7 +99,7 @@ impl NodeVersionManager { let mut response = match reqwest::blocking::get(&url) { Ok(response) => response, - Err(error) => return Err(VersionManagerError::DownloadError { url, error }), + Err(source) => return Err(VersionManagerError::DownloadError { url, source }), }; if !response.status().is_success() { @@ -112,19 +112,19 @@ impl NodeVersionManager { let mut file = match std::fs::File::create(download_file_path) { Ok(file) => file, - Err(error) => { + Err(source) => { return Err(VersionManagerError::FailedCreateFile { file: download_file_path.to_string_lossy().to_string(), - error, + source, }) } }; response .copy_to(&mut file) - .map_err(|error| VersionManagerError::FailedWriteFile { + .map_err(|source| VersionManagerError::FailedWriteFile { file: download_file_path.to_string_lossy().to_string(), - error, + source, })?; shuru::log!("Download complete."); @@ -151,10 +151,10 @@ impl NodeVersionManager { download_file_path: &std::path::Path, ) -> Result<(), VersionManagerError> { shuru::log!("Cleaning up the downloaded archive..."); - std::fs::remove_file(download_file_path).map_err(|error| { + std::fs::remove_file(download_file_path).map_err(|source| { VersionManagerError::FailedDeleteFile { file: download_file_path.to_string_lossy().to_string(), - error, + source, } }) } diff --git a/src/version_manager/python_version_manager.rs b/src/version_manager/python_version_manager.rs index de76970..b04466a 100644 --- a/src/version_manager/python_version_manager.rs +++ b/src/version_manager/python_version_manager.rs @@ -81,7 +81,7 @@ impl PythonVersionManager { let mut response = match reqwest::blocking::get(&url) { Ok(response) => response, - Err(error) => return Err(VersionManagerError::DownloadError { url, error }), + Err(source) => return Err(VersionManagerError::DownloadError { url, source }), }; if !response.status().is_success() { @@ -94,19 +94,19 @@ impl PythonVersionManager { let mut file = match std::fs::File::create(download_file_path) { Ok(file) => file, - Err(error) => { + Err(source) => { return Err(VersionManagerError::FailedCreateFile { file: download_file_path.to_string_lossy().to_string(), - error, + source, }) } }; response .copy_to(&mut file) - .map_err(|error| VersionManagerError::FailedWriteFile { + .map_err(|source| VersionManagerError::FailedWriteFile { file: download_file_path.to_string_lossy().to_string(), - error, + source, })?; shuru::log!("Download complete."); @@ -217,10 +217,10 @@ impl PythonVersionManager { .status() { Ok(status) => status, - Err(error) => { + Err(source) => { return Err(VersionManagerError::FailedRunCommand { command: format!("{:?}", cmd), - error, + source, }) } }; @@ -235,10 +235,10 @@ impl PythonVersionManager { } else { let output = match cmd.output() { Ok(output) => output, - Err(error) => { + Err(source) => { return Err(VersionManagerError::FailedRunCommand { command: format!("{:?}", cmd), - error, + source, }) } }; @@ -260,10 +260,10 @@ impl PythonVersionManager { download_file_path: &std::path::Path, ) -> Result<(), VersionManagerError> { shuru::log!("Cleaning up the downloaded archive..."); - std::fs::remove_file(download_file_path).map_err(|error| { + std::fs::remove_file(download_file_path).map_err(|source| { VersionManagerError::FailedDeleteFile { file: download_file_path.to_string_lossy().to_string(), - error, + source, } }) }