diff options
Diffstat (limited to 'src/error.rs')
-rw-r--r-- | src/error.rs | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..afed111 --- /dev/null +++ b/src/error.rs | |||
@@ -0,0 +1,31 @@ | |||
1 | use std::error::Error; | ||
2 | use axum::http::StatusCode; | ||
3 | use axum::Json; | ||
4 | use axum::response::{IntoResponse, Response}; | ||
5 | use serde_json::json; | ||
6 | use tracing::error; | ||
7 | use crate::auth::AuthError; | ||
8 | |||
9 | pub enum WebolError { | ||
10 | Auth(AuthError), | ||
11 | Generic, | ||
12 | Server(Box<dyn Error>), | ||
13 | } | ||
14 | |||
15 | impl IntoResponse for WebolError { | ||
16 | fn into_response(self) -> Response { | ||
17 | let (status, error_message) = match self { | ||
18 | WebolError::Auth(err) => err.get(), | ||
19 | WebolError::Generic => (StatusCode::INTERNAL_SERVER_ERROR, ""), | ||
20 | WebolError::Server(err) => { | ||
21 | error!("server error: {}", err.to_string()); | ||
22 | (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") | ||
23 | }, | ||
24 | |||
25 | }; | ||
26 | let body = Json(json!({ | ||
27 | "error": error_message, | ||
28 | })); | ||
29 | (status, body).into_response() | ||
30 | } | ||
31 | } | ||