Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

axum-extra/protobuf: Use rejection macros for ProtobufRejection #3124

Merged
merged 1 commit into from
Dec 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 17 additions & 62 deletions axum-extra/src/protobuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use axum::{
extract::{rejection::BytesRejection, FromRequest, Request},
response::{IntoResponse, Response},
};
use axum_core::__composite_rejection as composite_rejection;
use axum_core::__define_rejection as define_rejection;
use bytes::{Bytes, BytesMut};
use http::StatusCode;
use prost::Message;
Expand Down Expand Up @@ -127,70 +129,23 @@ where
}
}

/// Rejection type for [`Protobuf`].
///
/// This rejection is used if the request body couldn't be decoded into the target type.
#[derive(Debug)]
pub struct ProtobufDecodeError(pub(crate) axum::Error);

impl ProtobufDecodeError {
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 {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Failed to decode the body: {:?}", self.0)
}
}

impl std::error::Error for ProtobufDecodeError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(&self.0)
}
}

impl IntoResponse for ProtobufDecodeError {
fn into_response(self) -> Response {
StatusCode::UNPROCESSABLE_ENTITY.into_response()
}
define_rejection! {
#[status = UNPROCESSABLE_ENTITY]
#[body = "Failed to decode the body"]
/// Rejection type for [`Protobuf`].
///
/// This rejection is used if the request body couldn't be decoded into the target type.
pub struct ProtobufDecodeError(Error);
}

/// 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(),
}
composite_rejection! {
/// Rejection used for [`Protobuf`].
///
/// Contains one variant for each way the [`Protobuf`] extractor
/// can fail.
pub enum ProtobufRejection {
ProtobufDecodeError,
BytesRejection,
}
}

Expand Down