mas_axum_utils/
sentry.rs

1// Copyright 2024 New Vector Ltd.
2// Copyright 2023, 2024 The Matrix.org Foundation C.I.C.
3//
4// SPDX-License-Identifier: AGPL-3.0-only
5// Please see LICENSE in the repository root for full details.
6
7use std::convert::Infallible;
8
9use axum::response::{IntoResponseParts, ResponseParts};
10use sentry::types::Uuid;
11
12/// A wrapper to include a Sentry event ID in the response headers.
13#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
14pub struct SentryEventID(Uuid);
15
16impl SentryEventID {
17    /// Create a new Sentry event ID header for the last event on the hub.
18    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/// Record an error. It will emit a tracing event with the error level if
40/// matches the pattern, warning otherwise. It also returns the Sentry event ID
41/// if the error was recorded.
42#[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        // With the `sentry-tracing` integration, Sentry should have
53        // captured an error, so let's extract the last event ID from the
54        // current hub
55        $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}