1use std::convert::Infallible;
8
9use axum::response::{IntoResponseParts, ResponseParts};
10use sentry::types::Uuid;
11
12#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
14pub struct SentryEventID(Uuid);
15
16impl SentryEventID {
17    pub fn for_last_event() -> Option<Self> {
19        sentry::last_event_id().map(Self)
20    }
21}
22
23impl From<Uuid> for SentryEventID {
24    fn from(uuid: Uuid) -> Self {
25        Self(uuid)
26    }
27}
28
29impl IntoResponseParts for SentryEventID {
30    type Error = Infallible;
31    fn into_response_parts(self, mut res: ResponseParts) -> Result<ResponseParts, Self::Error> {
32        res.headers_mut()
33            .insert("X-Sentry-Event-ID", self.0.to_string().parse().unwrap());
34
35        Ok(res)
36    }
37}
38
39#[macro_export]
43macro_rules! record_error {
44    ($error:expr, !) => {{
45        tracing::warn!(message = &$error as &dyn std::error::Error);
46        Option::<$crate::sentry::SentryEventID>::None
47    }};
48
49    ($error:expr) => {{
50        tracing::error!(message = &$error as &dyn std::error::Error);
51
52        $crate::sentry::SentryEventID::for_last_event()
56    }};
57
58    ($error:expr, $pattern:pat) => {
59        if let $pattern = $error {
60            record_error!($error)
61        } else {
62            record_error!($error, !)
63        }
64    };
65}