1use axum::{
7 http::StatusCode,
8 response::{IntoResponse, Response},
9};
10
11#[derive(thiserror::Error, Debug)]
12pub enum AppError {
13 #[error("not found")]
14 NotFound,
15
16 #[error("unauthorized")]
17 Unauthorized,
18
19 #[error("forbidden")]
20 Forbidden,
21
22 #[error("bad request: {0}")]
23 BadRequest(String),
24
25 #[error("payload too large: {0}")]
26 PayloadTooLarge(String),
27
28 #[error("internal server error: {0}")]
29 Internal(String),
30
31 #[error(transparent)]
32 Sqlx(#[from] sqlx::Error),
33
34 #[error(transparent)]
35 Session(#[from] tower_sessions::session::Error),
36
37 #[error(transparent)]
38 Template(#[from] askama::Error),
39
40 #[error(transparent)]
41 Mail(#[from] crate::mail::MailError),
42
43 #[error(transparent)]
44 Entries(#[from] crate::entries::EntriesError),
45
46 #[error(transparent)]
47 NflData(#[from] nfl_data::NflDataError),
48
49 #[error(transparent)]
50 Other(#[from] anyhow::Error),
51}
52
53impl From<axum_login::Error<crate::Db>> for AppError {
54 fn from(e: axum_login::Error<crate::Db>) -> Self {
55 match e {
56 axum_login::Error::Session(err) => Self::Session(err),
57 axum_login::Error::Backend(err) => err,
58 }
59 }
60}
61
62impl IntoResponse for AppError {
63 fn into_response(self) -> Response {
64 let status = match &self {
65 Self::NotFound => StatusCode::NOT_FOUND,
66 Self::Unauthorized => StatusCode::UNAUTHORIZED,
67 Self::Forbidden => StatusCode::FORBIDDEN,
68 Self::BadRequest(_) => StatusCode::BAD_REQUEST,
69 Self::PayloadTooLarge(_) => StatusCode::PAYLOAD_TOO_LARGE,
70 Self::Sqlx(sqlx::Error::RowNotFound) => StatusCode::NOT_FOUND,
71 Self::Sqlx(_)
72 | Self::Session(_)
73 | Self::Template(_)
74 | Self::Mail(_)
75 | Self::Entries(_)
76 | Self::Internal(_)
77 | Self::NflData(_)
78 | Self::Other(_) => StatusCode::INTERNAL_SERVER_ERROR,
79 };
80
81 if status.is_server_error() {
82 match &self {
83 Self::Other(error) => tracing::error!(
85 error = ?self,
86 source = %format!("{error:#}"),
87 status = %status,
88 "request failed"
89 ),
90 _ => tracing::error!(error = ?self, status = %status, "request failed"),
91 }
92 (status, "Internal server error").into_response()
93 } else {
94 tracing::debug!(error = ?self, status = %status, "request rejected");
95 (status, self.to_string()).into_response()
96 }
97 }
98}
99
100#[cfg(test)]
101mod tests {
102 use axum::body::to_bytes;
103 use axum::http::StatusCode;
104 use axum::response::IntoResponse;
105
106 use super::AppError;
107
108 async fn body_of(error: AppError) -> (StatusCode, String) {
109 let response = error.into_response();
110 let status = response.status();
111 let body = to_bytes(response.into_body(), usize::MAX).await.unwrap();
112 (status, String::from_utf8(body.to_vec()).unwrap())
113 }
114
115 #[tokio::test]
116 async fn server_errors_hide_detail_behind_a_generic_body() {
117 let (status, body) = body_of(AppError::Internal("SENTINEL_DETAIL".into())).await;
118 assert_eq!(status, StatusCode::INTERNAL_SERVER_ERROR);
119 assert_eq!(body, "Internal server error");
120 assert!(!body.contains("SENTINEL_DETAIL"));
121
122 let (status, body) = body_of(AppError::Other(anyhow::anyhow!("SENTINEL_CHAIN"))).await;
123 assert_eq!(status, StatusCode::INTERNAL_SERVER_ERROR);
124 assert_eq!(body, "Internal server error");
125 }
126
127 #[tokio::test]
128 async fn client_errors_keep_their_messages_and_statuses() {
129 let (status, body) = body_of(AppError::BadRequest("invalid season".into())).await;
130 assert_eq!(status, StatusCode::BAD_REQUEST);
131 assert_eq!(body, "bad request: invalid season");
132
133 let (status, body) = body_of(AppError::NotFound).await;
134 assert_eq!(status, StatusCode::NOT_FOUND);
135 assert_eq!(body, "not found");
136
137 let (status, _) = body_of(AppError::Unauthorized).await;
138 assert_eq!(status, StatusCode::UNAUTHORIZED);
139
140 let (status, _) = body_of(AppError::Forbidden).await;
141 assert_eq!(status, StatusCode::FORBIDDEN);
142
143 let (status, _) = body_of(AppError::PayloadTooLarge("huge".into())).await;
144 assert_eq!(status, StatusCode::PAYLOAD_TOO_LARGE);
145 }
146
147 #[tokio::test]
148 async fn missing_rows_stay_not_found() {
149 let (status, _) = body_of(AppError::Sqlx(sqlx::Error::RowNotFound)).await;
151 assert_eq!(status, StatusCode::NOT_FOUND);
152 }
153}