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/output.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/output.rs') diff --git a/src/output.rs b/src/output.rs index 8d71909..c00d4bb 100644 --- a/src/output.rs +++ b/src/output.rs @@ -3,8 +3,9 @@ use super::{ api, bt::{BuildTemplate, Traitline}, }; -use std::{error::Error, io, io::Write}; +use std::{io, io::Write}; use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor}; +use anyhow::Error; const HEADER_COLOR: Color = Color::Cyan; @@ -67,7 +68,7 @@ pub fn show_build_template(build: &BuildTemplate) -> io::Result<()> { /// Show an error to the standard error stream. /// /// This will also show the chain of errors that lead up to this error, if available. -pub fn show_error(error: &E) -> io::Result<()> { +pub fn show_error(error: Error) -> io::Result<()> { let mut error_color = ColorSpec::new(); error_color.set_fg(Some(Color::Red)); let mut stderr = StandardStream::stderr(ColorChoice::Auto); -- cgit v1.2.3