|
| 1 | +/// Emitt a tracing error event for an error with rich, structured context. |
| 2 | +/// It capturing the error using the experimental Rust [std::error::Report](https://doc.rust-lang.org/stable/std/error/struct.Report.html) |
| 3 | +/// and adding the type name as error.kind, the backtrace as error.trace and the error stack as error.message |
| 4 | +/// |
| 5 | +/// # Examples |
| 6 | +/// |
| 7 | +/// ```rust |
| 8 | +/// use prima_tracing::trace_error; |
| 9 | +/// # fn main() { |
| 10 | +/// |
| 11 | +/// let error = "not a number".parse::<usize>().unwrap_err(); |
| 12 | +/// trace_error!(error, "Parsing error!"); |
| 13 | +/// trace_error!(error, "Parsing error: {}", uid="1234"); |
| 14 | +/// # } |
| 15 | +/// ``` |
| 16 | +#[macro_export] |
| 17 | +macro_rules! trace_error { |
| 18 | + ($error:expr, $message:expr) => { |
| 19 | + { |
| 20 | + $crate::trace_error!($error, $message,) |
| 21 | + } |
| 22 | + }; |
| 23 | + ($error:expr, $message:expr, $($field:tt)*) => { |
| 24 | + { |
| 25 | + let kind = std::any::type_name_of_val(&$error); |
| 26 | + let error_message = format!("{:#}", $error); |
| 27 | + let stack = error_report::Report::new(&$error); |
| 28 | + let trace = std::backtrace::Backtrace::force_capture(); |
| 29 | + |
| 30 | + $crate::tracing::error!( |
| 31 | + { |
| 32 | + error.message = error_message, |
| 33 | + error.kind = kind, |
| 34 | + error.stack = ?stack, |
| 35 | + error.trace = %trace, |
| 36 | + $($field)* |
| 37 | + }, |
| 38 | + "{} {}", |
| 39 | + $message, |
| 40 | + $error |
| 41 | + ); |
| 42 | + } |
| 43 | + }; |
| 44 | + ($error:expr, $message:expr) => { |
| 45 | + { |
| 46 | + $crate::trace_error!($error, $message, {}) |
| 47 | + } |
| 48 | + }; |
| 49 | +} |
| 50 | + |
| 51 | +/// Emitt a tracing error event for anyhow error with rich, structured context. |
| 52 | +/// It capturing the error using the experimental Rust [std::error::Report](https://doc.rust-lang.org/stable/std/error/struct.Report.html) |
| 53 | +/// and adding the type name as error.kind, the backtrace as error.trace and the error stack as error.message |
| 54 | +/// |
| 55 | +/// # Examples |
| 56 | +/// |
| 57 | +/// ```rust |
| 58 | +/// use anyhow::anyhow |
| 59 | +/// use prima_tracing::trace_error; |
| 60 | +/// # fn main() { |
| 61 | +/// |
| 62 | +/// let error = anyhow!("an error"); |
| 63 | +/// trace_anyhow_error!(error, "Throw error!"); |
| 64 | +/// trace_anyhow_error!(error, "Throw error!", uid="1234"); |
| 65 | +/// # } |
| 66 | +/// ``` |
| 67 | +#[cfg(feature = "anyhow")] |
| 68 | +#[macro_export] |
| 69 | +macro_rules! trace_anyhow_error { |
| 70 | + ($error:expr, $message:expr) => { |
| 71 | + { |
| 72 | + $crate::trace_anyhow_error!($error, $message,) |
| 73 | + } |
| 74 | + }; |
| 75 | + ($error:expr, $message:expr, $($field:tt)*) => { |
| 76 | + { |
| 77 | + let kind = std::any::type_name_of_val(&$error.root_cause()); |
| 78 | + let error_message = format!("{:#}", $error); |
| 79 | + let std_err: &(dyn std::error::Error + 'static) = $error.as_ref(); |
| 80 | + let stack = error_report::Report::new(std_err); |
| 81 | + |
| 82 | + $crate::tracing::error!( |
| 83 | + { |
| 84 | + error.message = error_message, |
| 85 | + error.kind = kind, |
| 86 | + error.stack = ?stack, |
| 87 | + error.trace = %$error.backtrace(), |
| 88 | + $($field)* |
| 89 | + }, |
| 90 | + "{} {:?}", |
| 91 | + $message, |
| 92 | + $error |
| 93 | + ); |
| 94 | + } |
| 95 | + }; |
| 96 | + ($error:expr, $message:expr) => { |
| 97 | + { |
| 98 | + $crate::trace_anyhow_error!($error, $message,) |
| 99 | + } |
| 100 | + }; |
| 101 | +} |
0 commit comments