Skip to content
This repository was archived by the owner on Sep 10, 2024. It is now read-only.

Commit 0bb34ed

Browse files
committed
Add the Sentry event ID in error response headers
1 parent be90cbb commit 0bb34ed

File tree

19 files changed

+149
-56
lines changed

19 files changed

+149
-56
lines changed

crates/axum-utils/src/fancy_error.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ use axum::{
1919
};
2020
use mas_templates::ErrorContext;
2121

22+
use crate::sentry::SentryEventID;
23+
2224
pub struct FancyError {
2325
context: ErrorContext,
2426
}
@@ -59,9 +61,10 @@ impl<E: std::fmt::Debug + std::fmt::Display> From<E> for FancyError {
5961
impl IntoResponse for FancyError {
6062
fn into_response(self) -> Response {
6163
let error = format!("{:?}", self.context);
62-
sentry::capture_message(&error, sentry::Level::Error);
64+
let event_id = sentry::capture_message(&error, sentry::Level::Error);
6365
(
6466
StatusCode::INTERNAL_SERVER_ERROR,
67+
SentryEventID::from(event_id),
6568
Extension(self.context),
6669
error,
6770
)

crates/axum-utils/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub mod csrf;
2828
pub mod fancy_error;
2929
pub mod http_client_factory;
3030
pub mod jwt;
31+
pub mod sentry;
3132
pub mod session;
3233
pub mod user_authorization;
3334

crates/axum-utils/src/sentry.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2023 The Matrix.org Foundation C.I.C.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use std::convert::Infallible;
16+
17+
use axum::response::{IntoResponseParts, ResponseParts};
18+
use sentry::types::Uuid;
19+
20+
/// A wrapper to include a Sentry event ID in the response headers.
21+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
22+
pub struct SentryEventID(Uuid);
23+
24+
impl From<Uuid> for SentryEventID {
25+
fn from(uuid: Uuid) -> Self {
26+
Self(uuid)
27+
}
28+
}
29+
30+
impl IntoResponseParts for SentryEventID {
31+
type Error = Infallible;
32+
fn into_response_parts(self, mut res: ResponseParts) -> Result<ResponseParts, Self::Error> {
33+
res.headers_mut()
34+
.insert("X-Sentry-Event-ID", self.0.to_string().parse().unwrap());
35+
36+
Ok(res)
37+
}
38+
}

crates/handlers/src/compat/login.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use axum::{extract::State, response::IntoResponse, Json};
1616
use chrono::Duration;
1717
use hyper::StatusCode;
18+
use mas_axum_utils::sentry::SentryEventID;
1819
use mas_data_model::{CompatSession, CompatSsoLoginState, Device, TokenType, User};
1920
use mas_storage::{
2021
compat::{
@@ -169,8 +170,8 @@ impl_from_error_for_route!(mas_storage::RepositoryError);
169170

170171
impl IntoResponse for RouteError {
171172
fn into_response(self) -> axum::response::Response {
172-
sentry::capture_error(&self);
173-
match self {
173+
let event_id = sentry::capture_error(&self);
174+
let response = match self {
174175
Self::Internal(_) | Self::SessionNotFound => MatrixError {
175176
errcode: "M_UNKNOWN",
176177
error: "Internal server error",
@@ -198,8 +199,9 @@ impl IntoResponse for RouteError {
198199
error: "Invalid login token",
199200
status: StatusCode::FORBIDDEN,
200201
},
201-
}
202-
.into_response()
202+
};
203+
204+
(SentryEventID::from(event_id), response).into_response()
203205
}
204206
}
205207

crates/handlers/src/compat/login_sso_redirect.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use axum::{
1818
response::IntoResponse,
1919
};
2020
use hyper::StatusCode;
21+
use mas_axum_utils::sentry::SentryEventID;
2122
use mas_router::{CompatLoginSsoAction, CompatLoginSsoComplete, UrlBuilder};
2223
use mas_storage::{compat::CompatSsoLoginRepository, BoxClock, BoxRepository, BoxRng};
2324
use rand::distributions::{Alphanumeric, DistString};
@@ -51,8 +52,13 @@ impl_from_error_for_route!(mas_storage::RepositoryError);
5152

5253
impl IntoResponse for RouteError {
5354
fn into_response(self) -> axum::response::Response {
54-
sentry::capture_error(&self);
55-
(StatusCode::INTERNAL_SERVER_ERROR, format!("{self}")).into_response()
55+
let event_id = sentry::capture_error(&self);
56+
(
57+
StatusCode::INTERNAL_SERVER_ERROR,
58+
SentryEventID::from(event_id),
59+
format!("{self}"),
60+
)
61+
.into_response()
5662
}
5763
}
5864

crates/handlers/src/compat/logout.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use axum::{response::IntoResponse, Json, TypedHeader};
1616
use headers::{authorization::Bearer, Authorization};
1717
use hyper::StatusCode;
18+
use mas_axum_utils::sentry::SentryEventID;
1819
use mas_data_model::TokenType;
1920
use mas_storage::{
2021
compat::{CompatAccessTokenRepository, CompatSessionRepository},
@@ -45,8 +46,8 @@ impl_from_error_for_route!(mas_storage::RepositoryError);
4546

4647
impl IntoResponse for RouteError {
4748
fn into_response(self) -> axum::response::Response {
48-
sentry::capture_error(&self);
49-
match self {
49+
let event_id = sentry::capture_error(&self);
50+
let response = match self {
5051
Self::Internal(_) => MatrixError {
5152
errcode: "M_UNKNOWN",
5253
error: "Internal error",
@@ -62,8 +63,9 @@ impl IntoResponse for RouteError {
6263
error: "Invalid access token",
6364
status: StatusCode::UNAUTHORIZED,
6465
},
65-
}
66-
.into_response()
66+
};
67+
68+
(SentryEventID::from(event_id), response).into_response()
6769
}
6870
}
6971

crates/handlers/src/compat/refresh.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use axum::{extract::State, response::IntoResponse, Json};
1616
use chrono::Duration;
1717
use hyper::StatusCode;
18+
use mas_axum_utils::sentry::SentryEventID;
1819
use mas_data_model::{TokenFormatError, TokenType};
1920
use mas_storage::{
2021
compat::{CompatAccessTokenRepository, CompatRefreshTokenRepository, CompatSessionRepository},
@@ -52,8 +53,8 @@ pub enum RouteError {
5253

5354
impl IntoResponse for RouteError {
5455
fn into_response(self) -> axum::response::Response {
55-
sentry::capture_error(&self);
56-
match self {
56+
let event_id = sentry::capture_error(&self);
57+
let response = match self {
5758
Self::Internal(_) | Self::UnknownSession => MatrixError {
5859
errcode: "M_UNKNOWN",
5960
error: "Internal error",
@@ -64,8 +65,9 @@ impl IntoResponse for RouteError {
6465
error: "Invalid refresh token",
6566
status: StatusCode::UNAUTHORIZED,
6667
},
67-
}
68-
.into_response()
68+
};
69+
70+
(SentryEventID::from(event_id), response).into_response()
6971
}
7072
}
7173

crates/handlers/src/graphql/mod.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ use axum::{
2828
use futures_util::TryStreamExt;
2929
use headers::{authorization::Bearer, Authorization, ContentType, HeaderValue};
3030
use hyper::header::CACHE_CONTROL;
31-
use mas_axum_utils::{cookies::CookieJar, FancyError, SessionInfo, SessionInfoExt};
31+
use mas_axum_utils::{
32+
cookies::CookieJar, sentry::SentryEventID, FancyError, SessionInfo, SessionInfoExt,
33+
};
3234
use mas_data_model::User;
3335
use mas_graphql::{Requester, Schema};
3436
use mas_matrix::HomeserverConnection;
@@ -144,9 +146,9 @@ impl_from_error_for_route!(mas_storage::RepositoryError);
144146

145147
impl IntoResponse for RouteError {
146148
fn into_response(self) -> Response {
147-
sentry::capture_error(&self);
149+
let event_id = sentry::capture_error(&self);
148150

149-
match self {
151+
let response = match self {
150152
e @ (Self::Internal(_) | Self::LoadFailed) => {
151153
let error = async_graphql::Error::new_with_source(e);
152154
(
@@ -182,7 +184,9 @@ impl IntoResponse for RouteError {
182184
)
183185
.into_response()
184186
}
185-
}
187+
};
188+
189+
(SentryEventID::from(event_id), response).into_response()
186190
}
187191
}
188192

crates/handlers/src/oauth2/authorization/complete.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use axum::{
1717
response::{Html, IntoResponse, Response},
1818
};
1919
use hyper::StatusCode;
20-
use mas_axum_utils::{cookies::CookieJar, csrf::CsrfExt, SessionInfoExt};
20+
use mas_axum_utils::{cookies::CookieJar, csrf::CsrfExt, sentry::SentryEventID, SessionInfoExt};
2121
use mas_data_model::{AuthorizationGrant, BrowserSession, Client, Device};
2222
use mas_keystore::Keystore;
2323
use mas_policy::{EvaluationResult, Policy};
@@ -53,9 +53,9 @@ pub enum RouteError {
5353

5454
impl IntoResponse for RouteError {
5555
fn into_response(self) -> axum::response::Response {
56-
sentry::capture_error(&self);
56+
let event = sentry::capture_error(&self);
5757
// TODO: better error pages
58-
match self {
58+
let response = match self {
5959
RouteError::NotFound => {
6060
(StatusCode::NOT_FOUND, "authorization grant was not found").into_response()
6161
}
@@ -67,7 +67,9 @@ impl IntoResponse for RouteError {
6767
RouteError::Internal(_) | Self::NoSuchClient => {
6868
(StatusCode::INTERNAL_SERVER_ERROR, self.to_string()).into_response()
6969
}
70-
}
70+
};
71+
72+
(SentryEventID::from(event), response).into_response()
7173
}
7274
}
7375

crates/handlers/src/oauth2/authorization/mod.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use axum::{
1717
response::{Html, IntoResponse, Response},
1818
};
1919
use hyper::StatusCode;
20-
use mas_axum_utils::{cookies::CookieJar, csrf::CsrfExt, SessionInfoExt};
20+
use mas_axum_utils::{cookies::CookieJar, csrf::CsrfExt, sentry::SentryEventID, SessionInfoExt};
2121
use mas_data_model::{AuthorizationCode, Pkce};
2222
use mas_keystore::Keystore;
2323
use mas_policy::Policy;
@@ -64,9 +64,9 @@ pub enum RouteError {
6464

6565
impl IntoResponse for RouteError {
6666
fn into_response(self) -> axum::response::Response {
67-
sentry::capture_error(&self);
67+
let event_id = sentry::capture_error(&self);
6868
// TODO: better error pages
69-
match self {
69+
let response = match self {
7070
RouteError::Internal(e) => {
7171
(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()).into_response()
7272
}
@@ -84,7 +84,9 @@ impl IntoResponse for RouteError {
8484
format!("Invalid redirect URI ({e})"),
8585
)
8686
.into_response(),
87-
}
87+
};
88+
89+
(SentryEventID::from(event_id), response).into_response()
8890
}
8991
}
9092

0 commit comments

Comments
 (0)