From 8510ed3e43ff0a92565815b02645cc7bf5cb6b18 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Thu, 7 Oct 2021 23:19:31 +0200 Subject: Modernize error handling This sprinkles in some thiserror and anyhow instead of the hand-rolled 'error_froms!' macros and the MainResult. The benefit of this is that we're hooking into an established ecosystem of error handling and we're saving a lot of effort in the hand-written Display and Error implementations. The reason for not sprinkling anyhow everywhere is because the retrieval/rendering part of kondou could be split off into a library at some point, in which case we want to have a proper KondouError type. However, the argument could be made that the current split of errors is not very good, especially because many of them boil down to the same inner errors (RenderError wrapping ApiError wrapping reqwest::Error), which keeps unnecessary information. Some future improvements may include 1.) Unifying those error enums into one bigger enum 2.) Attaching more context through anyhow in the application layer 3.) Properly define an API and split off the inner logic from the UI logic --- src/cache.rs | 28 +++++----------------------- 1 file changed, 5 insertions(+), 23 deletions(-) (limited to 'src/cache.rs') diff --git a/src/cache.rs b/src/cache.rs index 09c8512..7dd65cf 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -1,33 +1,15 @@ //! Caching support to prevent hitting the API a lot. -use std::{error::Error, fmt, fs, path::Path}; +use std::{fs, path::Path}; +use thiserror::Error; use xdg::BaseDirectories; use super::APP_NAME; -#[derive(Debug)] +#[derive(Error, Debug)] pub enum CacheError { - Io(std::io::Error), -} - -error_froms! { CacheError, - err: std::io::Error => CacheError::Io(err), -} - -impl fmt::Display for CacheError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match *self { - CacheError::Io(_) => write!(f, "cache input/output error"), - } - } -} - -impl Error for CacheError { - fn source(&self) -> Option<&(dyn Error + 'static)> { - match *self { - CacheError::Io(ref err) => Some(err), - } - } + #[error("Cache I/O error")] + Io(#[from] std::io::Error), } /// A generic cache. -- cgit v1.2.3