Macro to record an HTTP response error with the Sentry event ID attached
This commit is contained in:
@@ -13,6 +13,13 @@ use sentry::types::Uuid;
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct SentryEventID(Uuid);
|
||||
|
||||
impl SentryEventID {
|
||||
/// Create a new Sentry event ID header for the last event on the hub.
|
||||
pub fn for_last_event() -> Option<Self> {
|
||||
sentry::last_event_id().map(Self)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Uuid> for SentryEventID {
|
||||
fn from(uuid: Uuid) -> Self {
|
||||
Self(uuid)
|
||||
@@ -28,3 +35,28 @@ impl IntoResponseParts for SentryEventID {
|
||||
Ok(res)
|
||||
}
|
||||
}
|
||||
|
||||
/// Record an error. It will emit a tracing event with the error level if
|
||||
/// matches the pattern, warning otherwise. It also returns the Sentry event ID
|
||||
/// if the error was recorded.
|
||||
#[macro_export]
|
||||
macro_rules! record_error {
|
||||
($error:expr, !) => {{
|
||||
tracing::warn!(message = &$error as &dyn std::error::Error);
|
||||
Option::<$crate::sentry::SentryEventID>::None
|
||||
}};
|
||||
|
||||
($error:expr, $pattern:pat) => {
|
||||
if let $pattern = $error {
|
||||
tracing::error!(message = &$error as &dyn std::error::Error);
|
||||
|
||||
// With the `sentry-tracing` integration, Sentry should have
|
||||
// captured an error, so let's extract the last event ID from the
|
||||
// current hub
|
||||
$crate::sentry::SentryEventID::for_last_event()
|
||||
} else {
|
||||
tracing::warn!(message = &$error as &dyn std::error::Error);
|
||||
None
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user