Merge pull request #692 from matthiasbeyer/libimagerror/map_err_trace
Add Result helpers for trace()ing errors
This commit is contained in:
commit
0d59305199
1 changed files with 46 additions and 0 deletions
|
@ -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 })
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue