Merge pull request #692 from matthiasbeyer/libimagerror/map_err_trace

Add Result helpers for trace()ing errors
This commit is contained in:
Matthias Beyer 2016-09-05 20:30:44 +02:00 committed by GitHub
commit 0d59305199

View file

@ -87,3 +87,49 @@ fn print_trace_dbg(idx: u64, e: &Error) {
}
}
/// 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 {
fn map_err_trace(self) -> Self;
fn map_err_dbg_trace(self) -> Self;
fn map_err_trace_exit(self, code: i32) -> Self;
fn map_err_trace_maxdepth(self, max: u64) -> Self;
}
impl<U, E: Error> MapErrTrace for Result<U, E> {
/// 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 })
}
/// Simply call `trace_error_exit(code)` on the Err (if there is one).
///
/// This does not return if there is an Err(e).
fn map_err_trace_exit(self, code: i32) -> Self {
self.map_err(|e| { trace_error_exit(&e, code) })
}
/// 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 })
}
}