diff options
author | Sam Tay <sam.chong.tay@gmail.com> | 2020-06-08 20:55:03 -0700 |
---|---|---|
committer | Sam Tay <sam.chong.tay@gmail.com> | 2020-06-08 20:55:03 -0700 |
commit | 546fc51006e2bcb9acf301a8c06a49a1d982bce1 (patch) | |
tree | 0625e60befafe11292255b4cffceef2c11c39494 /src/error.rs | |
parent | 613e1196b5e89d320283c4cea44a5d450616a932 (diff) |
Refactor error handling
Diffstat (limited to 'src/error.rs')
-rw-r--r-- | src/error.rs | 111 |
1 files changed, 24 insertions, 87 deletions
diff --git a/src/error.rs b/src/error.rs index 9563359..d125fb2 100644 --- a/src/error.rs +++ b/src/error.rs @@ -2,95 +2,32 @@ use std::path::PathBuf; pub type Result<T, E = Error> = std::result::Result<T, E>; -#[derive(Debug)] -pub struct Error { - #[allow(dead_code)] - pub kind: ErrorKind, - pub error: String, -} - -#[derive(Debug)] -pub enum ErrorKind { - Malformed, - StackExchange, - Permissions, - OperatingSystem, - Panic, +#[derive(thiserror::Error, Debug)] +pub enum Error { + #[error("Termimad error: {0}")] + Termimad(#[from] termimad::Error), + #[error("Crossterm error: {0}")] + Crossterm(#[from] crossterm::ErrorKind), + #[error("Reqwest error: {0}")] + Reqwest(#[from] reqwest::Error), + #[error("IO error: {0}")] + IO(#[from] std::io::Error), + #[error("File `{}` is malformed; try removing it", .0.display())] + MalformedFile(PathBuf), + #[error("Lacking {0:?} permissions on `{}`", .1.display())] + Permissions(PermissionType, PathBuf), + #[error("{0}")] + StackExchange(String), + #[error("Couldn't find a suitable project directory; is your OS supported?")] + ProjectDir, + #[error("Empty sites file in cache")] EmptySites, + #[error("Sorry, couldn't find any answers for your query")] NoResults, - Termimad(termimad::Error), } -impl From<&str> for Error { - fn from(err: &str) -> Self { - Error { - kind: ErrorKind::Panic, - error: String::from(err), - } - } -} - -impl From<termimad::Error> for Error { - fn from(err: termimad::Error) -> Self { - Error { - kind: ErrorKind::Termimad(err), - error: String::from(""), - } - } -} - -// TODO add others -impl Error { - pub fn malformed(path: &PathBuf) -> Self { - Error { - kind: ErrorKind::Malformed, - error: format!("File `{}` is malformed; try removing it.", path.display()), - } - } - pub fn se(err: String) -> Self { - Error { - kind: ErrorKind::StackExchange, - error: err, - } - } - pub fn create_dir(path: &PathBuf) -> Self { - Error { - kind: ErrorKind::Permissions, - error: format!( - "Couldn't create directory `{}`; please check the permissions - on the parent directory", - path.display() - ), - } - } - pub fn create_file(path: &PathBuf) -> Self { - Error { - kind: ErrorKind::Permissions, - error: format!( - "Couldn't create file `{}`; please check the directory permissions", - path.display() - ), - } - } - pub fn write_file(path: &PathBuf) -> Self { - Error { - kind: ErrorKind::Permissions, - error: format!( - "Couldn't write to file `{}`; please check its permissions", - path.display() - ), - } - } - pub fn os(err: &str) -> Self { - Error { - kind: ErrorKind::OperatingSystem, - error: String::from(err), - } - } - pub fn no_results() -> Self { - Error { - kind: ErrorKind::NoResults, - error: String::from("Sorry, no answers found for your question. Try another query."), - } - } +#[derive(Debug)] +pub enum PermissionType { + Create, + Write, } |