Rewrite libimagerror to be thin layer over error-chain

This commit is contained in:
Matthias Beyer 2018-02-08 14:59:15 +01:00
parent 365d0a1cb8
commit e481dbfd12
4 changed files with 17 additions and 120 deletions

View file

@ -20,5 +20,4 @@ is-it-maintained-open-issues = { repository = "matthiasbeyer/imag" }
maintenance = { status = "actively-developed" }
[dependencies]
log = "0.4.0"
ansi_term = "0.10"
error-chain = "0.11"

View file

@ -17,9 +17,10 @@
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
use std::error::Error;
use error_chain::ChainedError;
/// An iterator that maps `f` over the `Error` elements of `iter`, similar to `std::iter::Map`.
/// An iterator that maps `f` over the `ChainedError` elements of `iter`, similar to
/// `std::iter::Map`.
///
/// This `struct` is created by the `on_err()` method on `TraceIterator`. See its
/// documentation for more information.
@ -121,11 +122,11 @@ impl<I, F, T, E> DoubleEndedIterator for UnwrapWith<I, F> where
/// Iterator helper for Unwrap with exiting on error
pub struct UnwrapExit<I, T, E>(I, i32)
where I: Iterator<Item = Result<T, E>>,
E: Error;
E: ChainedError;
impl<I, T, E> Iterator for UnwrapExit<I, T, E>
where I: Iterator<Item = Result<T, E>>,
E: Error
E: ChainedError
{
type Item = T;
@ -137,7 +138,7 @@ impl<I, T, E> Iterator for UnwrapExit<I, T, E>
impl<I, T, E> DoubleEndedIterator for UnwrapExit<I, T, E>
where I: DoubleEndedIterator<Item = Result<T, E>>,
E: Error
E: ChainedError
{
fn next_back(&mut self) -> Option<Self::Item> {
use trace::MapErrTrace;
@ -154,10 +155,10 @@ pub trait TraceIterator<T, E> : Iterator<Item = Result<T, E>> + Sized {
/// nothing will be passed to `::trace::trace_error`, no matter how many `Err` items might
/// be present.
#[inline]
fn trace_unwrap(self) -> UnwrapWith<Self, fn(E)> where E: Error {
fn trace_unwrap<K>(self) -> UnwrapWith<Self, fn(E)> where E: ChainedError<ErrorKind = K> {
#[inline]
fn trace_error<E: Error>(err: E) {
::trace::trace_error(&err);
fn trace_error<K, E: ChainedError<ErrorKind = K>>(err: E) {
eprintln!("{}", err.display_chain());
}
self.unwrap_with(trace_error)
@ -172,7 +173,7 @@ pub trait TraceIterator<T, E> : Iterator<Item = Result<T, E>> + Sized {
/// be present.
#[inline]
fn trace_unwrap_exit(self, exitcode: i32) -> UnwrapExit<Self, T, E>
where E: Error
where E: ChainedError
{
UnwrapExit(self, exitcode)
}

View file

@ -33,8 +33,7 @@
while_true,
)]
#[macro_use] extern crate log;
extern crate ansi_term;
extern crate error_chain;
pub mod trace;
pub mod iter;

View file

@ -17,94 +17,8 @@
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
use std::error::Error;
use std::io::Write;
use std::io::stderr;
use ansi_term::Colour::Red;
/// Print an Error type and its cause recursively
///
/// The error is printed with "Error NNNN :" as prefix, where "NNNN" is a number which increases
/// which each recursion into the errors cause. The error description is used to visualize what
/// failed and if there is a cause "-- caused by:" is appended, and the cause is printed on the next
/// line.
///
/// Example output:
///
/// ```ignore
/// Error 1 : Some error -- caused by:
/// Error 2 : Some other error -- caused by:
/// Error 3 : Yet another Error -- caused by:
/// ...
///
/// Error <NNNN> : <Error description>
/// ```
pub fn trace_error(e: &Error) {
print_trace_maxdepth(count_error_causes(e), e, ::std::u64::MAX);
write!(stderr(), "\n").ok();
}
/// Convenience function: calls `trace_error()` with `e` and afterwards `std::process::exit()`
/// with `code`
pub fn trace_error_exit(e: &Error, code: i32) -> ! {
use std::process::exit;
debug!("Tracing error...");
trace_error(e);
debug!("Calling exit()");
exit(code);
}
/// Print an Error type and its cause recursively, but only `max` levels
///
/// Output is the same as for `trace_error()`, though there are only `max` levels printed.
pub fn trace_error_maxdepth(e: &Error, max: u64) {
let n = count_error_causes(e);
let msg = Red.blink().paint(format!("{}/{} Levels of errors will be printed\n",
(if max > n { n } else { max }), n));
write!(stderr(), "{}", msg).ok();
print_trace_maxdepth(n, e, max);
write!(stderr(), "").ok();
}
/// Print an Error type and its cause recursively with the debug!() macro
///
/// Output is the same as for `trace_error()`.
pub fn trace_error_dbg(e: &Error) {
print_trace_dbg(0, e);
}
/// Helper function for `trace_error()` and `trace_error_maxdepth()`.
///
/// Returns the cause of the last processed error in the recursion, so `None` if all errors where
/// processed.
fn print_trace_maxdepth(idx: u64, e: &Error, max: u64) -> Option<&Error> {
if e.cause().is_some() && idx > 0 {
e.cause().map(|cause| {
match print_trace_maxdepth(idx - 1, cause, max) {
None => write!(stderr(), "\n").ok(),
Some(_) => write!(stderr(), " -- caused:\n").ok(),
};
});
} else {
write!(stderr(), "\n").ok();
}
write!(stderr(), "{}: {}", Red.paint(format!("ERROR[{:>4}]", idx)), e).ok();
e.cause()
}
/// Count errors in `Error::cause()` recursively
fn count_error_causes(e: &Error) -> u64 {
1 + e.cause().map(|c| count_error_causes(c)).unwrap_or(0)
}
fn print_trace_dbg(idx: u64, e: &Error) {
debug!("{}: {}", Red.blink().paint(format!("ERROR[{:>4}]", idx)), e);
if e.cause().is_some() {
e.cause().map(|c| print_trace_dbg(idx + 1, c));
}
}
use error_chain::ChainedError;
/// Helper functions for `Result<T, E>` types to reduce overhead in the following situations:
///
@ -117,38 +31,22 @@ pub trait MapErrTrace {
type Output;
fn map_err_trace(self) -> Self;
fn map_err_dbg_trace(self) -> Self;
fn map_err_trace_exit_unwrap(self, code: i32) -> Self::Output;
fn map_err_trace_maxdepth(self, max: u64) -> Self;
}
impl<U, E: Error> MapErrTrace for Result<U, E> {
impl<U, K, E: ChainedError<ErrorKind = K>> MapErrTrace for Result<U, E> {
type Output = U;
/// Simply call `trace_error()` on the Err (if there is one) and return the error.
///
/// This does nothing besides the side effect of printing the error trace
fn map_err_trace(self) -> Self {
self.map_err(|e| { trace_error(&e); e })
}
/// Simply call `trace_error_dbg()` on the Err (if there is one) and return the error.
///
/// This does nothing besides the side effect of printing the error trace
fn map_err_dbg_trace(self) -> Self {
self.map_err(|e| { trace_error_dbg(&e); e })
self.map_err(|e| { eprintln!("{}", e.display_chain()); e })
}
/// Trace the error and exit or unwrap the Ok(_).
fn map_err_trace_exit_unwrap(self, code: i32) -> Self::Output {
self.map_err(|e| { trace_error_exit(&e, code) }).unwrap()
}
/// Simply call `trace_error_maxdepth(max)` on the Err (if there is one) and return the error.
///
/// This does nothing besides the side effect of printing the error trace to a certain depth
fn map_err_trace_maxdepth(self, max: u64) -> Self {
self.map_err(|e| { trace_error_maxdepth(&e, max); e })
self.map_err(|e| { eprintln!("{}", e.display_chain()); exit(code) }).unwrap()
}
}