2016-10-01 15:35:06 +00:00
|
|
|
//
|
|
|
|
// imag - the personal information management suite for the commandline
|
2019-01-03 01:32:07 +00:00
|
|
|
// Copyright (C) 2015-2019 Matthias Beyer <mail@beyermatthias.de> and contributors
|
2016-10-01 15:35:06 +00:00
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; version
|
|
|
|
// 2.1 of the License.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
//
|
|
|
|
|
2018-02-08 13:59:15 +00:00
|
|
|
use std::process::exit;
|
2018-02-08 14:38:03 +00:00
|
|
|
use std::fmt::Display;
|
|
|
|
use std::fmt::Formatter;
|
|
|
|
use std::fmt::Result as FmtResult;
|
2018-10-30 17:40:50 +00:00
|
|
|
use failure::Error;
|
2018-02-08 14:38:03 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-30 17:40:50 +00:00
|
|
|
impl<'a> Display for ImagTrace<'a, Error>
|
2018-02-08 14:38:03 +00:00
|
|
|
{
|
|
|
|
fn fmt(&self, fmt: &mut Formatter) -> FmtResult {
|
2018-10-30 17:40:50 +00:00
|
|
|
let _ = writeln!(fmt, "{}: {}", Red.blink().paint("ERROR[ 0]"), self.0)?;
|
|
|
|
|
|
|
|
{
|
|
|
|
for (i, cause) in self.0.iter_causes().enumerate() {
|
|
|
|
let _ = writeln!(fmt,
|
|
|
|
"{prefix}: {error}",
|
|
|
|
prefix = Red.blink().paint(format!("ERROR[{:>4}]", i)),
|
|
|
|
error = cause)?;
|
|
|
|
}
|
2018-02-08 14:38:03 +00:00
|
|
|
}
|
|
|
|
|
2018-10-30 17:40:50 +00:00
|
|
|
let _ = writeln!(fmt, "{}", Red.paint("--- BACKTRACE ---"))?;
|
|
|
|
let _ = writeln!(fmt, "{:?}", self.0.backtrace())?;
|
2018-02-08 14:38:03 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-06 23:01:36 +00:00
|
|
|
|
2018-10-30 17:40:50 +00:00
|
|
|
pub fn trace_error(e: &Error) {
|
2018-02-08 14:38:03 +00:00
|
|
|
eprintln!("{}", ImagTrace::new(e));
|
2018-02-08 14:02:25 +00:00
|
|
|
}
|
|
|
|
|
2018-10-30 17:40:50 +00:00
|
|
|
pub fn trace_error_dbg(e: &Error) {
|
|
|
|
debug!("{}", ImagTrace::new(e));
|
2018-02-08 14:02:25 +00:00
|
|
|
}
|
|
|
|
|
2016-09-04 14:45:14 +00:00
|
|
|
/// Helper functions for `Result<T, E>` types to reduce overhead in the following situations:
|
|
|
|
///
|
|
|
|
/// ```ignore
|
|
|
|
/// function().map_err(|e| { trace_error(&e); e })
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// and variants
|
|
|
|
pub trait MapErrTrace {
|
2017-10-12 17:02:37 +00:00
|
|
|
type Output;
|
|
|
|
|
2016-09-04 14:45:14 +00:00
|
|
|
fn map_err_trace(self) -> Self;
|
2017-10-12 17:02:37 +00:00
|
|
|
fn map_err_trace_exit_unwrap(self, code: i32) -> Self::Output;
|
2016-09-04 14:45:14 +00:00
|
|
|
}
|
|
|
|
|
2018-10-30 17:40:50 +00:00
|
|
|
impl<U> MapErrTrace for Result<U, Error> {
|
2017-10-12 17:02:37 +00:00
|
|
|
type Output = U;
|
2016-09-04 14:45:14 +00:00
|
|
|
|
|
|
|
/// 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 {
|
2018-02-08 14:02:25 +00:00
|
|
|
self.map_err(|e| { trace_error(&e); e })
|
2016-09-04 14:45:14 +00:00
|
|
|
}
|
|
|
|
|
2018-01-22 11:48:28 +00:00
|
|
|
/// Trace the error and exit or unwrap the Ok(_).
|
2017-10-12 17:02:37 +00:00
|
|
|
fn map_err_trace_exit_unwrap(self, code: i32) -> Self::Output {
|
2018-02-08 14:02:25 +00:00
|
|
|
self.map_err(|e| { trace_error(&e); exit(code) }).unwrap()
|
2016-09-04 14:45:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|