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/bt.rs | 40 ++++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 22 deletions(-) (limited to 'src/bt.rs') diff --git a/src/bt.rs b/src/bt.rs index c379d1d..3c1b604 100644 --- a/src/bt.rs +++ b/src/bt.rs @@ -1,42 +1,38 @@ use super::api::{Api, ApiError, Profession, Skill, Specialization}; use byteorder::{ReadBytesExt, WriteBytesExt, LE}; use num_enum::{IntoPrimitive, TryFromPrimitive}; -use std::{convert::TryFrom, error::Error, fmt, io::Cursor, str::FromStr}; +use std::{convert::TryFrom, fmt, io::Cursor, str::FromStr}; +use thiserror::Error; -#[derive(Debug)] +#[derive(Error, Debug)] pub enum ChatlinkError { - ApiError(ApiError), + #[error("Error accessing the API")] + ApiError(#[from] ApiError), + #[error("The input link is malformed")] MalformedInput, } -error_froms! { ChatlinkError, - err: ApiError => ChatlinkError::ApiError(err), - _err: base64::DecodeError => ChatlinkError::MalformedInput, - _err: num_enum::TryFromPrimitiveError => ChatlinkError::MalformedInput, - _err: num_enum::TryFromPrimitiveError => ChatlinkError::MalformedInput, -} - impl From for ChatlinkError { fn from(_err: std::io::Error) -> Self { panic!("The reading cursor should never return an error!"); } } -impl fmt::Display for ChatlinkError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match *self { - ChatlinkError::ApiError(_) => write!(f, "error accessing the API"), - ChatlinkError::MalformedInput => write!(f, "the input link is malformed"), - } +impl From for ChatlinkError { + fn from(_: base64::DecodeError) -> Self { + ChatlinkError::MalformedInput } } -impl Error for ChatlinkError { - fn source(&self) -> Option<&(dyn Error + 'static)> { - match *self { - ChatlinkError::ApiError(ref err) => Some(err), - _ => None, - } +impl From> for ChatlinkError { + fn from(_: num_enum::TryFromPrimitiveError) -> Self { + ChatlinkError::MalformedInput + } +} + +impl From> for ChatlinkError { + fn from(_: num_enum::TryFromPrimitiveError) -> Self { + ChatlinkError::MalformedInput } } -- cgit v1.2.3