mirror of
https://github.com/tokio-rs/axum.git
synced 2025-03-19 21:49:04 +01:00
axum-extra/protobuf: Use rejection macros for ProtobufRejection
(#3124)
This commit is contained in:
parent
e58b166f0e
commit
bb5cf4ce5a
1 changed files with 17 additions and 62 deletions
|
@ -4,6 +4,8 @@ use axum::{
|
||||||
extract::{rejection::BytesRejection, FromRequest, Request},
|
extract::{rejection::BytesRejection, FromRequest, Request},
|
||||||
response::{IntoResponse, Response},
|
response::{IntoResponse, Response},
|
||||||
};
|
};
|
||||||
|
use axum_core::__composite_rejection as composite_rejection;
|
||||||
|
use axum_core::__define_rejection as define_rejection;
|
||||||
use bytes::{Bytes, BytesMut};
|
use bytes::{Bytes, BytesMut};
|
||||||
use http::StatusCode;
|
use http::StatusCode;
|
||||||
use prost::Message;
|
use prost::Message;
|
||||||
|
@ -127,70 +129,23 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Rejection type for [`Protobuf`].
|
define_rejection! {
|
||||||
///
|
#[status = UNPROCESSABLE_ENTITY]
|
||||||
/// This rejection is used if the request body couldn't be decoded into the target type.
|
#[body = "Failed to decode the body"]
|
||||||
#[derive(Debug)]
|
/// Rejection type for [`Protobuf`].
|
||||||
pub struct ProtobufDecodeError(pub(crate) axum::Error);
|
///
|
||||||
|
/// This rejection is used if the request body couldn't be decoded into the target type.
|
||||||
impl ProtobufDecodeError {
|
pub struct ProtobufDecodeError(Error);
|
||||||
pub(crate) fn from_err<E>(err: E) -> Self
|
|
||||||
where
|
|
||||||
E: Into<axum::BoxError>,
|
|
||||||
{
|
|
||||||
Self(axum::Error::new(err))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::fmt::Display for ProtobufDecodeError {
|
composite_rejection! {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
/// Rejection used for [`Protobuf`].
|
||||||
write!(f, "Failed to decode the body: {:?}", self.0)
|
///
|
||||||
}
|
/// Contains one variant for each way the [`Protobuf`] extractor
|
||||||
}
|
/// can fail.
|
||||||
|
pub enum ProtobufRejection {
|
||||||
impl std::error::Error for ProtobufDecodeError {
|
ProtobufDecodeError,
|
||||||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
BytesRejection,
|
||||||
Some(&self.0)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl IntoResponse for ProtobufDecodeError {
|
|
||||||
fn into_response(self) -> Response {
|
|
||||||
StatusCode::UNPROCESSABLE_ENTITY.into_response()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Rejection used for [`Protobuf`].
|
|
||||||
///
|
|
||||||
/// Contains one variant for each way the [`Protobuf`] extractor
|
|
||||||
/// can fail.
|
|
||||||
#[derive(Debug)]
|
|
||||||
#[non_exhaustive]
|
|
||||||
pub enum ProtobufRejection {
|
|
||||||
#[allow(missing_docs)]
|
|
||||||
ProtobufDecodeError(ProtobufDecodeError),
|
|
||||||
#[allow(missing_docs)]
|
|
||||||
BytesRejection(BytesRejection),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<ProtobufDecodeError> for ProtobufRejection {
|
|
||||||
fn from(inner: ProtobufDecodeError) -> Self {
|
|
||||||
Self::ProtobufDecodeError(inner)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<BytesRejection> for ProtobufRejection {
|
|
||||||
fn from(inner: BytesRejection) -> Self {
|
|
||||||
Self::BytesRejection(inner)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl IntoResponse for ProtobufRejection {
|
|
||||||
fn into_response(self) -> Response {
|
|
||||||
match self {
|
|
||||||
Self::ProtobufDecodeError(inner) => inner.into_response(),
|
|
||||||
Self::BytesRejection(inner) => inner.into_response(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue