aboutsummaryrefslogtreecommitdiff
path: root/src/error.rs
diff options
context:
space:
mode:
authorFxQnLr <[email protected]>2024-02-25 15:53:04 +0100
committerGitHub <[email protected]>2024-02-25 15:53:04 +0100
commitf0dc13f907a72ffef44f89b5e197567db129b020 (patch)
treed560273df2eece276cbda021cb4e95c044bb19df /src/error.rs
parentc663810817183c8f92a4279236ca84d271365088 (diff)
parent91cd665671d564620bce13e693cd7ecaad697db9 (diff)
downloadwebol-f0dc13f907a72ffef44f89b5e197567db129b020.tar
webol-f0dc13f907a72ffef44f89b5e197567db129b020.tar.gz
webol-f0dc13f907a72ffef44f89b5e197567db129b020.zip
Merge pull request #17 from FxQnLr/0.3.2
0.3.2
Diffstat (limited to 'src/error.rs')
-rw-r--r--src/error.rs88
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 @@
1use std::io; 1use ::ipnetwork::IpNetworkError;
2use axum::http::header::ToStrError;
2use axum::http::StatusCode; 3use axum::http::StatusCode;
3use axum::Json;
4use axum::response::{IntoResponse, Response}; 4use axum::response::{IntoResponse, Response};
5use axum::Json;
6use mac_address::MacParseError;
5use serde_json::json; 7use serde_json::json;
8use std::io;
6use tracing::error; 9use tracing::error;
7use crate::auth::Error as AuthError;
8 10
9#[derive(Debug)] 11#[derive(Debug, thiserror::Error)]
10pub enum Error { 12pub 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
19impl IntoResponse for Error { 50impl 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,