From 546fc51006e2bcb9acf301a8c06a49a1d982bce1 Mon Sep 17 00:00:00 2001 From: Sam Tay Date: Mon, 8 Jun 2020 20:55:03 -0700 Subject: Refactor error handling --- src/error.rs | 111 +++++++++++++---------------------------------------------- 1 file changed, 24 insertions(+), 87 deletions(-) (limited to 'src/error.rs') 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 = std::result::Result; -#[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 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, } -- cgit v1.2.3