Skip to content

Commit e58b166

Browse files
authored
axum-extra/multipart: Use rejection macros for MultipartRejection (#3123)
1 parent 5f82540 commit e58b166

File tree

1 file changed

+14
-89
lines changed

1 file changed

+14
-89
lines changed

axum-extra/src/extract/multipart.rs

+14-89
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use axum::{
88
response::{IntoResponse, Response},
99
RequestExt,
1010
};
11+
use axum_core::__composite_rejection as composite_rejection;
12+
use axum_core::__define_rejection as define_rejection;
1113
use futures_util::stream::Stream;
1214
use http::{
1315
header::{HeaderMap, CONTENT_TYPE},
@@ -313,100 +315,23 @@ fn parse_boundary(headers: &HeaderMap) -> Option<String> {
313315
multer::parse_boundary(content_type).ok()
314316
}
315317

316-
/// Rejection used for [`Multipart`].
317-
///
318-
/// Contains one variant for each way the [`Multipart`] extractor can fail.
319-
#[derive(Debug)]
320-
#[non_exhaustive]
321-
pub enum MultipartRejection {
322-
#[allow(missing_docs)]
323-
InvalidBoundary(InvalidBoundary),
324-
}
325-
326-
impl IntoResponse for MultipartRejection {
327-
fn into_response(self) -> Response {
328-
match self {
329-
Self::InvalidBoundary(inner) => inner.into_response(),
330-
}
331-
}
332-
}
333-
334-
impl MultipartRejection {
335-
/// Get the response body text used for this rejection.
336-
pub fn body_text(&self) -> String {
337-
match self {
338-
Self::InvalidBoundary(inner) => inner.body_text(),
339-
}
340-
}
341-
342-
/// Get the status code used for this rejection.
343-
pub fn status(&self) -> http::StatusCode {
344-
match self {
345-
Self::InvalidBoundary(inner) => inner.status(),
346-
}
347-
}
348-
}
349-
350-
impl From<InvalidBoundary> for MultipartRejection {
351-
fn from(inner: InvalidBoundary) -> Self {
352-
Self::InvalidBoundary(inner)
353-
}
354-
}
355-
356-
impl std::fmt::Display for MultipartRejection {
357-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
358-
match self {
359-
Self::InvalidBoundary(inner) => write!(f, "{}", inner.body_text()),
360-
}
361-
}
362-
}
363-
364-
impl std::error::Error for MultipartRejection {
365-
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
366-
match self {
367-
Self::InvalidBoundary(inner) => Some(inner),
368-
}
369-
}
370-
}
371-
372-
/// Rejection type used if the `boundary` in a `multipart/form-data` is
373-
/// missing or invalid.
374-
#[derive(Debug, Default)]
375-
#[non_exhaustive]
376-
pub struct InvalidBoundary;
377-
378-
impl IntoResponse for InvalidBoundary {
379-
fn into_response(self) -> Response {
380-
let body = self.body_text();
381-
axum_core::__log_rejection!(
382-
rejection_type = Self,
383-
body_text = body,
384-
status = self.status(),
385-
);
386-
(self.status(), body).into_response()
387-
}
388-
}
389-
390-
impl InvalidBoundary {
391-
/// Get the response body text used for this rejection.
392-
pub fn body_text(&self) -> String {
393-
"Invalid `boundary` for `multipart/form-data` request".into()
394-
}
395-
396-
/// Get the status code used for this rejection.
397-
pub fn status(&self) -> http::StatusCode {
398-
http::StatusCode::BAD_REQUEST
318+
composite_rejection! {
319+
/// Rejection used for [`Multipart`].
320+
///
321+
/// Contains one variant for each way the [`Multipart`] extractor can fail.
322+
pub enum MultipartRejection {
323+
InvalidBoundary,
399324
}
400325
}
401326

402-
impl std::fmt::Display for InvalidBoundary {
403-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
404-
write!(f, "{}", self.body_text())
405-
}
327+
define_rejection! {
328+
#[status = BAD_REQUEST]
329+
#[body = "Invalid `boundary` for `multipart/form-data` request"]
330+
/// Rejection type used if the `boundary` in a `multipart/form-data` is
331+
/// missing or invalid.
332+
pub struct InvalidBoundary;
406333
}
407334

408-
impl std::error::Error for InvalidBoundary {}
409-
410335
#[cfg(test)]
411336
mod tests {
412337
use super::*;

0 commit comments

Comments
 (0)