diff options
author | FxQnLr <[email protected]> | 2024-07-02 12:59:26 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2024-07-02 12:59:26 +0000 |
commit | 99d03d16faf83258e7674d2b767ad62a34fe219c (patch) | |
tree | b6bccb03702b0301305193acdb572e51cd286e78 /src | |
parent | e4a1dac094556f0a4000b429897402facc09e03f (diff) | |
parent | 9329d197f37b68c1800cc7c81a741491c3cd0c6c (diff) | |
download | webol-99d03d16faf83258e7674d2b767ad62a34fe219c.tar webol-99d03d16faf83258e7674d2b767ad62a34fe219c.tar.gz webol-99d03d16faf83258e7674d2b767ad62a34fe219c.zip |
Merge pull request #41 from FxQnLr/server-error
Give more useful error responses and logs
Diffstat (limited to 'src')
-rw-r--r-- | src/error.rs | 51 |
1 files changed, 27 insertions, 24 deletions
diff --git a/src/error.rs b/src/error.rs index b8a078b..1b4be19 100644 --- a/src/error.rs +++ b/src/error.rs | |||
@@ -1,13 +1,12 @@ | |||
1 | use ::ipnetwork::IpNetworkError; | 1 | use ipnetwork::IpNetworkError; |
2 | use axum::http::header::ToStrError; | ||
3 | use axum::http::StatusCode; | 2 | use axum::http::StatusCode; |
4 | use axum::response::{IntoResponse, Response}; | 3 | use axum::response::{IntoResponse, Response}; |
5 | use axum::Json; | 4 | use axum::Json; |
6 | use mac_address::MacParseError; | 5 | use mac_address::MacParseError; |
7 | use serde_json::json; | 6 | use serde_json::json; |
8 | use utoipa::ToSchema; | ||
9 | use std::io; | 7 | use std::io; |
10 | use tracing::{error, warn}; | 8 | use tracing::{error, warn}; |
9 | use utoipa::ToSchema; | ||
11 | 10 | ||
12 | #[derive(Debug, thiserror::Error, ToSchema)] | 11 | #[derive(Debug, thiserror::Error, ToSchema)] |
13 | pub enum Error { | 12 | pub enum Error { |
@@ -23,12 +22,6 @@ pub enum Error { | |||
23 | source: std::num::ParseIntError, | 22 | source: std::num::ParseIntError, |
24 | }, | 23 | }, |
25 | 24 | ||
26 | #[error("header parse: {source}")] | ||
27 | ParseHeader { | ||
28 | #[from] | ||
29 | source: ToStrError, | ||
30 | }, | ||
31 | |||
32 | #[error("string parse: {source}")] | 25 | #[error("string parse: {source}")] |
33 | IpParse { | 26 | IpParse { |
34 | #[from] | 27 | #[from] |
@@ -55,6 +48,15 @@ impl IntoResponse for Error { | |||
55 | fn into_response(self) -> Response { | 48 | fn into_response(self) -> Response { |
56 | let (status, error_message) = match self { | 49 | let (status, error_message) = match self { |
57 | Self::Json { source } => { | 50 | Self::Json { source } => { |
51 | // !THIS REALLY SHOULD NOT HAPPEN!: | ||
52 | // Json file has to had been tampered with by an external force | ||
53 | error!("{source}"); | ||
54 | (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") | ||
55 | } | ||
56 | Self::ParseInt { source } => { | ||
57 | // !THIS REALLY SHOULD NOT HAPPEN!: | ||
58 | // Mac Address `&str` can't be converted to hex, which should be impossible trough | ||
59 | // `MacAddress` type-check on device registration and edit | ||
58 | error!("{source}"); | 60 | error!("{source}"); |
59 | (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") | 61 | (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") |
60 | } | 62 | } |
@@ -67,25 +69,26 @@ impl IntoResponse for Error { | |||
67 | (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") | 69 | (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") |
68 | } | 70 | } |
69 | } | 71 | } |
70 | Self::ParseHeader { source } => { | ||
71 | error!("{source}"); | ||
72 | (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") | ||
73 | } | ||
74 | Self::ParseInt { source } => { | ||
75 | error!("{source}"); | ||
76 | (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") | ||
77 | } | ||
78 | Self::MacParse { source } => { | 72 | Self::MacParse { source } => { |
79 | error!("{source}"); | 73 | warn!("{source}"); |
80 | (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") | 74 | ( |
75 | StatusCode::INTERNAL_SERVER_ERROR, | ||
76 | "The given MAC-Address couldn't be parsed", | ||
77 | ) | ||
81 | } | 78 | } |
82 | Self::IpParse { source } => { | 79 | Self::IpParse { source } => { |
83 | error!("{source}"); | 80 | warn!("{source}"); |
84 | (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") | 81 | ( |
85 | }, | 82 | StatusCode::INTERNAL_SERVER_ERROR, |
83 | "The given IP-Address couldn't be parsed", | ||
84 | ) | ||
85 | } | ||
86 | Self::NoIpOnPing => { | 86 | Self::NoIpOnPing => { |
87 | error!("Ping requested but no ip given"); | 87 | warn!("Ping requested but no ip given"); |
88 | (StatusCode::BAD_REQUEST, "No Ip saved for requested device, but device started") | 88 | ( |
89 | StatusCode::BAD_REQUEST, | ||
90 | "No IP saved for device, ping can't be executed. Device may be started anyway", | ||
91 | ) | ||
89 | } | 92 | } |
90 | }; | 93 | }; |
91 | let body = Json(json!({ | 94 | let body = Json(json!({ |