diff options
author | FxQnLr <[email protected]> | 2024-02-15 17:17:30 +0100 |
---|---|---|
committer | FxQnLr <[email protected]> | 2024-02-15 17:17:30 +0100 |
commit | 3bc7cf8ed36016ca3da9438a98f4fe8b8e6f9e61 (patch) | |
tree | 48a38a52ebfc41fadab439b9a87fae3d43f78790 /src/error.rs | |
parent | c663810817183c8f92a4279236ca84d271365088 (diff) | |
download | webol-3bc7cf8ed36016ca3da9438a98f4fe8b8e6f9e61.tar webol-3bc7cf8ed36016ca3da9438a98f4fe8b8e6f9e61.tar.gz webol-3bc7cf8ed36016ca3da9438a98f4fe8b8e6f9e61.zip |
Closes #10 & #12. Added `thiserror` crate and changed to `IntoSocketAddr` for easier usage and error handling
Diffstat (limited to 'src/error.rs')
-rw-r--r-- | src/error.rs | 65 |
1 files changed, 40 insertions, 25 deletions
diff --git a/src/error.rs b/src/error.rs index 56d6c52..4f1bedd 100644 --- a/src/error.rs +++ b/src/error.rs | |||
@@ -1,44 +1,59 @@ | |||
1 | use std::io; | 1 | use crate::auth::Error as AuthError; |
2 | use axum::http::StatusCode; | 2 | use axum::http::StatusCode; |
3 | use axum::Json; | ||
4 | use axum::response::{IntoResponse, Response}; | 3 | use axum::response::{IntoResponse, Response}; |
4 | use axum::Json; | ||
5 | use serde_json::json; | 5 | use serde_json::json; |
6 | use std::io; | ||
6 | use tracing::error; | 7 | use tracing::error; |
7 | use crate::auth::Error as AuthError; | ||
8 | 8 | ||
9 | #[derive(Debug)] | 9 | #[derive(Debug, thiserror::Error)] |
10 | pub enum Error { | 10 | pub enum Error { |
11 | #[error("generic error")] | ||
11 | Generic, | 12 | Generic, |
12 | Auth(AuthError), | 13 | |
13 | DB(sqlx::Error), | 14 | #[error("auth: {source}")] |
14 | IpParse(<std::net::IpAddr as std::str::FromStr>::Err), | 15 | Auth { |
15 | BufferParse(std::num::ParseIntError), | 16 | #[from] |
16 | Broadcast(io::Error), | 17 | source: AuthError, |
18 | }, | ||
19 | |||
20 | #[error("db: {source}")] | ||
21 | Db { | ||
22 | #[from] | ||
23 | source: sqlx::Error, | ||
24 | }, | ||
25 | |||
26 | #[error("buffer parse: {source}")] | ||
27 | ParseInt { | ||
28 | #[from] | ||
29 | source: std::num::ParseIntError, | ||
30 | }, | ||
31 | |||
32 | #[error("io: {source}")] | ||
33 | Io { | ||
34 | #[from] | ||
35 | source: io::Error, | ||
36 | }, | ||
17 | } | 37 | } |
18 | 38 | ||
19 | impl IntoResponse for Error { | 39 | impl IntoResponse for Error { |
20 | fn into_response(self) -> Response { | 40 | fn into_response(self) -> Response { |
41 | error!("{}", self.to_string()); | ||
21 | let (status, error_message) = match self { | 42 | let (status, error_message) = match self { |
22 | Self::Auth(err) => { | 43 | Self::Auth { source } => source.get(), |
23 | err.get() | ||
24 | }, | ||
25 | Self::Generic => (StatusCode::INTERNAL_SERVER_ERROR, ""), | 44 | Self::Generic => (StatusCode::INTERNAL_SERVER_ERROR, ""), |
26 | Self::IpParse(err) => { | 45 | Self::Db { source } => { |
27 | error!("server error: {}", err.to_string()); | 46 | error!("{source}"); |
28 | (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") | ||
29 | }, | ||
30 | Self::DB(err) => { | ||
31 | error!("server error: {}", err.to_string()); | ||
32 | (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") | 47 | (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") |
33 | }, | 48 | } |
34 | Self::Broadcast(err) => { | 49 | Self::Io { source } => { |
35 | error!("server error: {}", err.to_string()); | 50 | error!("{source}"); |
36 | (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") | 51 | (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") |
37 | }, | 52 | } |
38 | Self::BufferParse(err) => { | 53 | Self::ParseInt { source } => { |
39 | error!("server error: {}", err.to_string()); | 54 | error!("{source}"); |
40 | (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") | 55 | (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") |
41 | }, | 56 | } |
42 | }; | 57 | }; |
43 | let body = Json(json!({ | 58 | let body = Json(json!({ |
44 | "error": error_message, | 59 | "error": error_message, |