diff options
author | Daniel Schadt <kingdread@gmx.de> | 2021-10-07 23:19:31 +0200 |
---|---|---|
committer | Daniel Schadt <kingdread@gmx.de> | 2021-10-07 23:19:31 +0200 |
commit | 8510ed3e43ff0a92565815b02645cc7bf5cb6b18 (patch) | |
tree | 7dc820e537583705099cf1f77b8a49e100b9a64d /src/render.rs | |
parent | cd110bcc5db993ae329d85674590f9a453014f8c (diff) | |
download | kondou-8510ed3e43ff0a92565815b02645cc7bf5cb6b18.tar.gz kondou-8510ed3e43ff0a92565815b02645cc7bf5cb6b18.tar.bz2 kondou-8510ed3e43ff0a92565815b02645cc7bf5cb6b18.zip |
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
Diffstat (limited to 'src/render.rs')
-rw-r--r-- | src/render.rs | 38 |
1 files changed, 7 insertions, 31 deletions
diff --git a/src/render.rs b/src/render.rs index af5d2b2..484fd83 100644 --- a/src/render.rs +++ b/src/render.rs @@ -8,42 +8,18 @@ use imageproc::{drawing, rect::Rect}; use num_traits::{Num, NumCast}; use rusttype::{Font, Scale}; use serde::{Deserialize, Serialize}; -use std::{error::Error, fmt}; +use thiserror::Error; -#[derive(Debug)] +#[derive(Error, Debug)] pub enum RenderError { - ApiError(ApiError), - ImageError(image::ImageError), + #[error("Error accessing the API")] + ApiError(#[from] ApiError), + #[error("Image processing error")] + ImageError(#[from] image::ImageError), + #[error("The build template contains nothing worth rendering")] EmptyBuild, } -error_froms! { RenderError, - err: ApiError => RenderError::ApiError(err), - err: image::ImageError => RenderError::ImageError(err), -} - -impl fmt::Display for RenderError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match *self { - RenderError::ApiError(_) => write!(f, "error accessing the API"), - RenderError::ImageError(_) => write!(f, "image processing error"), - RenderError::EmptyBuild => { - write!(f, "the build template contains nothing worth rendering") - } - } - } -} - -impl Error for RenderError { - fn source(&self) -> Option<&(dyn Error + 'static)> { - match *self { - RenderError::ApiError(ref err) => Some(err), - RenderError::ImageError(ref err) => Some(err), - _ => None, - } - } -} - #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] pub enum Alignment { Left, |