Skip to content

Commit

Permalink
feat: add source to VersionManagerError errors (#32)
Browse files Browse the repository at this point in the history
Replaced technical errors from the machine with the attribute source from the error crate.
  • Loading branch information
andcscott authored Oct 18, 2024
1 parent 99f6307 commit dc1b2bd
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 28 deletions.
37 changes: 27 additions & 10 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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}")]
Expand Down
14 changes: 7 additions & 7 deletions src/version_manager/node_version_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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.");
Expand All @@ -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,
}
})
}
Expand Down
22 changes: 11 additions & 11 deletions src/version_manager/python_version_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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.");
Expand Down Expand Up @@ -217,10 +217,10 @@ impl PythonVersionManager {
.status()
{
Ok(status) => status,
Err(error) => {
Err(source) => {
return Err(VersionManagerError::FailedRunCommand {
command: format!("{:?}", cmd),
error,
source,
})
}
};
Expand All @@ -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,
})
}
};
Expand All @@ -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,
}
})
}
Expand Down

0 comments on commit dc1b2bd

Please sign in to comment.