From 7c631abbc607468e6796a1d47aa7d9da48d6340e Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Tue, 2 Jul 2024 14:54:55 +0200 Subject: Closes #27. Give more useful error responses and logs --- src/error.rs | 51 +++++++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 24 deletions(-) (limited to 'src/error.rs') 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 @@ -use ::ipnetwork::IpNetworkError; -use axum::http::header::ToStrError; +use ipnetwork::IpNetworkError; use axum::http::StatusCode; use axum::response::{IntoResponse, Response}; use axum::Json; use mac_address::MacParseError; use serde_json::json; -use utoipa::ToSchema; use std::io; use tracing::{error, warn}; +use utoipa::ToSchema; #[derive(Debug, thiserror::Error, ToSchema)] pub enum Error { @@ -23,12 +22,6 @@ pub enum Error { source: std::num::ParseIntError, }, - #[error("header parse: {source}")] - ParseHeader { - #[from] - source: ToStrError, - }, - #[error("string parse: {source}")] IpParse { #[from] @@ -55,6 +48,15 @@ impl IntoResponse for Error { fn into_response(self) -> Response { let (status, error_message) = match self { Self::Json { source } => { + // !THIS REALLY SHOULD NOT HAPPEN!: + // Json file has to had been tampered with by an external force + error!("{source}"); + (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") + } + Self::ParseInt { source } => { + // !THIS REALLY SHOULD NOT HAPPEN!: + // Mac Address `&str` can't be converted to hex, which should be impossible trough + // `MacAddress` type-check on device registration and edit error!("{source}"); (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") } @@ -67,25 +69,26 @@ impl IntoResponse for Error { (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") } } - Self::ParseHeader { source } => { - error!("{source}"); - (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") - } - Self::ParseInt { source } => { - error!("{source}"); - (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") - } Self::MacParse { source } => { - error!("{source}"); - (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") + warn!("{source}"); + ( + StatusCode::INTERNAL_SERVER_ERROR, + "The given MAC-Address couldn't be parsed", + ) } Self::IpParse { source } => { - error!("{source}"); - (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") - }, + warn!("{source}"); + ( + StatusCode::INTERNAL_SERVER_ERROR, + "The given IP-Address couldn't be parsed", + ) + } Self::NoIpOnPing => { - error!("Ping requested but no ip given"); - (StatusCode::BAD_REQUEST, "No Ip saved for requested device, but device started") + warn!("Ping requested but no ip given"); + ( + StatusCode::BAD_REQUEST, + "No IP saved for device, ping can't be executed. Device may be started anyway", + ) } }; let body = Json(json!({ -- cgit v1.2.3