diff options
Diffstat (limited to 'src/error.rs')
-rw-r--r-- | src/error.rs | 88 |
1 files changed, 62 insertions, 26 deletions
diff --git a/src/error.rs b/src/error.rs index 56d6c52..513b51b 100644 --- a/src/error.rs +++ b/src/error.rs | |||
@@ -1,44 +1,80 @@ | |||
1 | use std::io; | 1 | use ::ipnetwork::IpNetworkError; |
2 | use axum::http::header::ToStrError; | ||
2 | use axum::http::StatusCode; | 3 | use axum::http::StatusCode; |
3 | use axum::Json; | ||
4 | use axum::response::{IntoResponse, Response}; | 4 | use axum::response::{IntoResponse, Response}; |
5 | use axum::Json; | ||
6 | use mac_address::MacParseError; | ||
5 | use serde_json::json; | 7 | use serde_json::json; |
8 | use std::io; | ||
6 | use tracing::error; | 9 | use tracing::error; |
7 | use crate::auth::Error as AuthError; | ||
8 | 10 | ||
9 | #[derive(Debug)] | 11 | #[derive(Debug, thiserror::Error)] |
10 | pub enum Error { | 12 | pub enum Error { |
11 | Generic, | 13 | #[error("db: {source}")] |
12 | Auth(AuthError), | 14 | Db { |
13 | DB(sqlx::Error), | 15 | #[from] |
14 | IpParse(<std::net::IpAddr as std::str::FromStr>::Err), | 16 | source: sqlx::Error, |
15 | BufferParse(std::num::ParseIntError), | 17 | }, |
16 | Broadcast(io::Error), | 18 | |
19 | #[error("buffer parse: {source}")] | ||
20 | ParseInt { | ||
21 | #[from] | ||
22 | source: std::num::ParseIntError, | ||
23 | }, | ||
24 | |||
25 | #[error("header parse: {source}")] | ||
26 | ParseHeader { | ||
27 | #[from] | ||
28 | source: ToStrError, | ||
29 | }, | ||
30 | |||
31 | #[error("string parse: {source}")] | ||
32 | IpParse { | ||
33 | #[from] | ||
34 | source: IpNetworkError, | ||
35 | }, | ||
36 | |||
37 | #[error("mac parse: {source}")] | ||
38 | MacParse { | ||
39 | #[from] | ||
40 | source: MacParseError, | ||
41 | }, | ||
42 | |||
43 | #[error("io: {source}")] | ||
44 | Io { | ||
45 | #[from] | ||
46 | source: io::Error, | ||
47 | }, | ||
17 | } | 48 | } |
18 | 49 | ||
19 | impl IntoResponse for Error { | 50 | impl IntoResponse for Error { |
20 | fn into_response(self) -> Response { | 51 | fn into_response(self) -> Response { |
52 | error!("{}", self.to_string()); | ||
21 | let (status, error_message) = match self { | 53 | let (status, error_message) = match self { |
22 | Self::Auth(err) => { | 54 | Self::Db { source } => { |
23 | err.get() | 55 | error!("{source}"); |
24 | }, | 56 | (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") |
25 | Self::Generic => (StatusCode::INTERNAL_SERVER_ERROR, ""), | 57 | } |
26 | Self::IpParse(err) => { | 58 | Self::Io { source } => { |
27 | error!("server error: {}", err.to_string()); | 59 | error!("{source}"); |
60 | (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") | ||
61 | } | ||
62 | Self::ParseHeader { source } => { | ||
63 | error!("{source}"); | ||
28 | (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") | 64 | (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") |
29 | }, | 65 | } |
30 | Self::DB(err) => { | 66 | Self::ParseInt { source } => { |
31 | error!("server error: {}", err.to_string()); | 67 | error!("{source}"); |
32 | (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") | 68 | (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") |
33 | }, | 69 | } |
34 | Self::Broadcast(err) => { | 70 | Self::MacParse { source } => { |
35 | error!("server error: {}", err.to_string()); | 71 | error!("{source}"); |
36 | (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") | 72 | (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") |
37 | }, | 73 | } |
38 | Self::BufferParse(err) => { | 74 | Self::IpParse { source } => { |
39 | error!("server error: {}", err.to_string()); | 75 | error!("{source}"); |
40 | (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") | 76 | (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") |
41 | }, | 77 | } |
42 | }; | 78 | }; |
43 | let body = Json(json!({ | 79 | let body = Json(json!({ |
44 | "error": error_message, | 80 | "error": error_message, |