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.
67use std::convert::Infallible;
89use axum::response::{IntoResponseParts, ResponseParts};
10use sentry::types::Uuid;
1112/// A wrapper to include a Sentry event ID in the response headers.
13#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
14pub struct SentryEventID(Uuid);
1516impl SentryEventID {
17/// Create a new Sentry event ID header for the last event on the hub.
18pub fn for_last_event() -> Option<Self> {
19 sentry::last_event_id().map(Self)
20 }
21}
2223impl From<Uuid> for SentryEventID {
24fn from(uuid: Uuid) -> Self {
25Self(uuid)
26 }
27}
2829impl IntoResponseParts for SentryEventID {
30type Error = Infallible;
31fn 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());
3435Ok(res)
36 }
37}
3839/// 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, !) => {{
45tracing::warn!(message = &$error as &dyn std::error::Error);
46Option::<$crate::sentry::SentryEventID>::None
47 }};
4849 ($error:expr) => {{
50tracing::error!(message = &$error as &dyn std::error::Error);
5152// 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 }};
5758 ($error:expr, $pattern:pat) => {
59if let $pattern = $error {
60record_error!($error)
61 } else {
62record_error!($error, !)
63 }
64 };
65}