From 126424630159a7aaa93f5e0de3a86d540a1f2b7c Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 7 Mar 2020 16:16:56 +0100 Subject: Move libimagerror to thiserror In this patch, the error output (tracing) algorithm was changed, but should essentially print the same information. Signed-off-by: Matthias Beyer --- lib/core/libimagerror/Cargo.toml | 9 ++--- lib/core/libimagerror/src/errors.rs | 72 +++++++++++++++++++++---------------- lib/core/libimagerror/src/lib.rs | 3 +- lib/core/libimagerror/src/trace.rs | 52 +++++++++------------------ 4 files changed, 65 insertions(+), 71 deletions(-) diff --git a/lib/core/libimagerror/Cargo.toml b/lib/core/libimagerror/Cargo.toml index 3b18a2f6..2cea7cbd 100644 --- a/lib/core/libimagerror/Cargo.toml +++ b/lib/core/libimagerror/Cargo.toml @@ -20,7 +20,8 @@ is-it-maintained-open-issues = { repository = "matthiasbeyer/imag" } maintenance = { status = "actively-developed" } [dependencies] -log = "0.4.6" -ansi_term = "0.12" -anyhow = "1" - +log = "0.4.6" +toml = "0.5" +ansi_term = "0.12" +thiserror = "1" +anyhow = "1" diff --git a/lib/core/libimagerror/src/errors.rs b/lib/core/libimagerror/src/errors.rs index a1373d91..e74eedaf 100644 --- a/lib/core/libimagerror/src/errors.rs +++ b/lib/core/libimagerror/src/errors.rs @@ -17,88 +17,98 @@ // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // -#[derive(Debug, Clone, Eq, PartialEq, Fail)] -pub enum ErrorMsg { - #[fail(display = "IO Error")] - IO, +use thiserror::Error; - #[fail(display = "Locking error")] +pub type Result = ::std::result::Result; + +#[derive(Debug, Error)] +pub enum Error { + #[error("IO error")] + IO(#[from] ::std::io::Error), + + #[error("Unknown IO error")] + UnknownIO, + + #[error("Locking error")] LockError, - #[fail(display = "UTF8 error")] - UTF8Error, + #[error("Locking error")] + PoisonError, + + #[error("UTF8 error")] + UTF8Error(#[from] std::str::Utf8Error), - #[fail(display = "Error in external process")] + #[error("Error in external process")] ExternalProcessError, - #[fail(display = "File Error")] + #[error("File Error")] FileError, - #[fail(display = "File not copied")] + #[error("File not copied")] FileNotCopied, - #[fail(display = "File not created")] + #[error("File not created")] FileNotCreated, - #[fail(display = "File not found")] + #[error("File not found")] FileNotFound, - #[fail(display = "Fail not removed")] + #[error("Fail not removed")] FileNotRemoved, - #[fail(display = "Fail not renamed")] + #[error("Fail not renamed")] FileNotRenamed, - #[fail(display = "File not seeked")] + #[error("File not seeked")] FileNotSeeked, - #[fail(display = "File not written")] + #[error("File not written")] FileNotWritten, - #[fail(display = "Directory not created")] + #[error("Directory not created")] DirNotCreated, - #[fail(display = "Formatting error")] - FormatError, + #[error("Formatting error")] + FormatError(#[from] std::fmt::Error), - #[fail(display = "ID is locked")] + #[error("ID is locked")] IdLocked, - #[fail(display = "Error while converting values")] + #[error("Error while converting values")] ConversionError, - #[fail(display = "Entry exists already: {}", _0)] + #[error("Entry exists already: {0}")] EntryAlreadyExists(String), - #[fail(display = "Entry not found: {}", _0)] + #[error("Entry not found: {0}")] EntryNotFound(String), - #[fail(display = "Entry header error")] + #[error("Entry header error")] EntryHeaderError, - #[fail(display = "Entry header type error")] + #[error("Entry header type error")] EntryHeaderTypeError, - #[fail(display = "Entry header type error at '{}', expected '{}'", _0, _1)] + #[error("Entry header type error at '{0}', expected '{1}'")] EntryHeaderTypeError2(&'static str, &'static str), - #[fail(display = "Entry header read error")] + #[error("Entry header read error")] EntryHeaderReadError, - #[fail(display = "Entry header write error")] + #[error("Entry header write error")] EntryHeaderWriteError, - #[fail(display = "Entry header field missing: {}", _0)] + #[error("Entry header field missing: {0}")] EntryHeaderFieldMissing(&'static str), - #[fail(display = "Toml deserialization error")] + #[error("Toml deserialization error")] TomlDeserError, - #[fail(display = "Toml querying error")] + #[error("Toml querying error")] TomlQueryError, } diff --git a/lib/core/libimagerror/src/lib.rs b/lib/core/libimagerror/src/lib.rs index bacd831a..78d1e2c2 100644 --- a/lib/core/libimagerror/src/lib.rs +++ b/lib/core/libimagerror/src/lib.rs @@ -37,9 +37,10 @@ #[macro_use] extern crate log; extern crate ansi_term; +extern crate toml; +extern crate thiserror; extern crate anyhow; - pub mod errors; pub mod exit; pub mod trace; diff --git a/lib/core/libimagerror/src/trace.rs b/lib/core/libimagerror/src/trace.rs index f990f1b1..e7bb5a39 100644 --- a/lib/core/libimagerror/src/trace.rs +++ b/lib/core/libimagerror/src/trace.rs @@ -18,48 +18,30 @@ // use std::process::exit; -use std::fmt::Display; -use std::fmt::Formatter; -use std::fmt::Result as FmtResult; + use anyhow::Error; use ansi_term::Colour::Red; -struct ImagTrace<'a, T: 'a + ?Sized>(&'a T); - -impl<'a, T: 'a + ?Sized> ImagTrace<'a, T> { - fn new(d: &'a T) -> ImagTrace<'a, T> { - ImagTrace(d) - } -} - -impl<'a> Display for ImagTrace<'a, Error> -{ - fn fmt(&self, fmt: &mut Formatter) -> FmtResult { - writeln!(fmt, "{}: {}", Red.blink().paint("ERROR[ 0]"), self.0)?; - - { - for (i, cause) in self.0.iter_causes().enumerate() { - writeln!(fmt, - "{prefix}: {error}", - prefix = Red.blink().paint(format!("ERROR[{:>4}]", i + 1)), - error = cause)?; - } - } - - writeln!(fmt, "{}", Red.paint("--- BACKTRACE ---"))?; - writeln!(fmt, "{:?}", self.0.backtrace())?; - - Ok(()) - } -} - - pub fn trace_error(e: &Error) { - eprintln!("{}", ImagTrace::new(e)); + eprintln!("{}: {}", Red.blink().paint("ERROR[ 0]"), e); + let mut i = 0; + e.chain().skip(1).for_each(|cause| { + eprintln!("{prefix}: {error}", + prefix = Red.blink().paint(format!("ERROR[{:>4}]", i + 1)), + error = cause); + i += 1; + }); } pub fn trace_error_dbg(e: &Error) { - debug!("{}", ImagTrace::new(e)); + debug!("{}: {}", Red.blink().paint("ERROR[ 0]"), e); + let mut i = 0; + e.chain().skip(1).for_each(|cause| { + debug!("{prefix}: {error}", + prefix = Red.blink().paint(format!("ERROR[{:>4}]", i + 1)), + error = cause); + i += 1; + }); } /// Helper functions for `Result` types to reduce overhead in the following situations: -- cgit v1.2.3