From 2d3b94d37d86384157916f29c423d5020a75d96f Mon Sep 17 00:00:00 2001 From: ItsEthra <107059409+ItsEthra@users.noreply.github.com> Date: Sun, 3 Dec 2023 19:50:16 +0300 Subject: [PATCH] Moved HxError && implemented Error trait --- src/error.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 3 +++ src/responders.rs | 46 +++------------------------------------------- 3 files changed, 51 insertions(+), 43 deletions(-) create mode 100644 src/error.rs diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..d89d096 --- /dev/null +++ b/src/error.rs @@ -0,0 +1,45 @@ +use std::{error, fmt}; + +use axum_core::response::IntoResponse; +use http::{header::InvalidHeaderValue, StatusCode}; + +#[derive(Debug)] +pub enum HxError { + InvalidHeaderValue(InvalidHeaderValue), + + #[cfg(feature = "serde")] + #[cfg_attr(feature = "unstable", doc(cfg(feature = "serde")))] + Json(serde_json::Error), +} + +impl From for HxError { + fn from(value: InvalidHeaderValue) -> Self { + Self::InvalidHeaderValue(value) + } +} + +#[cfg(feature = "serde")] +#[cfg_attr(feature = "unstable", doc(cfg(feature = "serde")))] +impl From for HxError { + fn from(value: serde_json::Error) -> Self { + Self::Json(value) + } +} + +impl fmt::Display for HxError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + HxError::InvalidHeaderValue(err) => write!(f, "Invalid header value: {err}"), + #[cfg(feature = "serde")] + HxError::Json(err) => write!(f, "Json: {err}"), + } + } +} + +impl error::Error for HxError {} + +impl IntoResponse for HxError { + fn into_response(self) -> axum_core::response::Response { + (StatusCode::INTERNAL_SERVER_ERROR, self.to_string()).into_response() + } +} diff --git a/src/lib.rs b/src/lib.rs index a57fa29..439ff68 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,9 @@ #![doc = include_str!("../README.md")] #![forbid(unsafe_code)] +mod error; +pub use error::*; + pub mod extractors; #[cfg(feature = "guards")] #[cfg_attr(feature = "unstable", doc(cfg(feature = "guards")))] diff --git a/src/responders.rs b/src/responders.rs index 20a6160..fdc06e4 100644 --- a/src/responders.rs +++ b/src/responders.rs @@ -2,10 +2,10 @@ use std::{convert::Infallible, str::FromStr}; -use axum_core::response::{IntoResponse, IntoResponseParts, ResponseParts}; -use http::{header::InvalidHeaderValue, HeaderValue, StatusCode, Uri}; +use axum_core::response::{IntoResponseParts, ResponseParts}; +use http::{HeaderValue, Uri}; -use crate::headers; +use crate::{headers, HxError}; mod location; pub use location::*; @@ -309,43 +309,3 @@ impl From for HeaderValue { } } } - -#[derive(Debug)] -pub enum HxError { - InvalidHeaderValue(InvalidHeaderValue), - - #[cfg(feature = "serde")] - #[cfg_attr(feature = "unstable", doc(cfg(feature = "serde")))] - Json(serde_json::Error), -} - -impl From for HxError { - fn from(value: InvalidHeaderValue) -> Self { - Self::InvalidHeaderValue(value) - } -} - -#[cfg(feature = "serde")] -#[cfg_attr(feature = "unstable", doc(cfg(feature = "serde")))] -impl From for HxError { - fn from(value: serde_json::Error) -> Self { - Self::Json(value) - } -} - -impl IntoResponse for HxError { - fn into_response(self) -> axum_core::response::Response { - match self { - Self::InvalidHeaderValue(_) => { - (StatusCode::INTERNAL_SERVER_ERROR, "invalid header value").into_response() - } - - #[cfg(feature = "serde")] - Self::Json(_) => ( - StatusCode::INTERNAL_SERVER_ERROR, - "failed to serialize event", - ) - .into_response(), - } - } -}