Add helper for tracing error and exit or unwrap the value

This commit is contained in:
Matthias Beyer 2017-10-12 19:02:37 +02:00
parent ac704cab76
commit eb4681bf65

View file

@ -114,13 +114,17 @@ fn print_trace_dbg(idx: u64, e: &Error) {
/// ///
/// and variants /// and variants
pub trait MapErrTrace { pub trait MapErrTrace {
type Output;
fn map_err_trace(self) -> Self; fn map_err_trace(self) -> Self;
fn map_err_dbg_trace(self) -> Self; fn map_err_dbg_trace(self) -> Self;
fn map_err_trace_exit(self, code: i32) -> Self; fn map_err_trace_exit(self, code: i32) -> Self;
fn map_err_trace_exit_unwrap(self, code: i32) -> Self::Output;
fn map_err_trace_maxdepth(self, max: u64) -> Self; fn map_err_trace_maxdepth(self, max: u64) -> Self;
} }
impl<U, E: Error> MapErrTrace for Result<U, E> { impl<U, E: Error> MapErrTrace for Result<U, E> {
type Output = U;
/// Simply call `trace_error()` on the Err (if there is one) and return the error. /// Simply call `trace_error()` on the Err (if there is one) and return the error.
/// ///
@ -143,6 +147,11 @@ impl<U, E: Error> MapErrTrace for Result<U, E> {
self.map_err(|e| { trace_error_exit(&e, code) }) self.map_err(|e| { trace_error_exit(&e, code) })
} }
/// Helper for calling map_err_trace_exit(n).unwrap() in one call
fn map_err_trace_exit_unwrap(self, code: i32) -> Self::Output {
self.map_err_trace_exit(code).unwrap()
}
/// Simply call `trace_error_maxdepth(max)` on the Err (if there is one) and return the error. /// 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 /// This does nothing besides the side effect of printing the error trace to a certain depth