mirror of
https://github.com/robertwayne/axum-htmx
synced 2024-12-25 09:49:32 +01:00
Moved HxError && implemented Error trait
This commit is contained in:
parent
c1c54a17f5
commit
2d3b94d37d
3 changed files with 51 additions and 43 deletions
45
src/error.rs
Normal file
45
src/error.rs
Normal file
|
@ -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<InvalidHeaderValue> for HxError {
|
||||
fn from(value: InvalidHeaderValue) -> Self {
|
||||
Self::InvalidHeaderValue(value)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "serde")]
|
||||
#[cfg_attr(feature = "unstable", doc(cfg(feature = "serde")))]
|
||||
impl From<serde_json::Error> 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()
|
||||
}
|
||||
}
|
|
@ -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")))]
|
||||
|
|
|
@ -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<SwapOption> 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<InvalidHeaderValue> for HxError {
|
||||
fn from(value: InvalidHeaderValue) -> Self {
|
||||
Self::InvalidHeaderValue(value)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "serde")]
|
||||
#[cfg_attr(feature = "unstable", doc(cfg(feature = "serde")))]
|
||||
impl From<serde_json::Error> 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(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue