diff options
author | fxqnlr <[email protected]> | 2024-07-02 14:54:55 +0200 |
---|---|---|
committer | fxqnlr <[email protected]> | 2024-07-02 14:54:55 +0200 |
commit | 7c631abbc607468e6796a1d47aa7d9da48d6340e (patch) | |
tree | 85958b2041e641cd91bca6606471b2c19f88d417 /src | |
parent | e3752e54cb0e77128a2c2583c23ddaa312d38a5f (diff) | |
download | webol-7c631abbc607468e6796a1d47aa7d9da48d6340e.tar webol-7c631abbc607468e6796a1d47aa7d9da48d6340e.tar.gz webol-7c631abbc607468e6796a1d47aa7d9da48d6340e.zip |
Closes #27. 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!({ |