Skip to content

Commit

Permalink
axum-extra/form: Add FailedToDeserializeFormBody rejection variant (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Turbo87 authored Dec 27, 2024
1 parent 53370b2 commit 09841ff
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions axum-extra/src/extract/form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,22 @@ where
type Rejection = FormRejection;

async fn from_request(req: Request, _state: &S) -> Result<Self, Self::Rejection> {
let is_get_or_head =
req.method() == http::Method::GET || req.method() == http::Method::HEAD;

let RawForm(bytes) = req.extract().await?;

let deserializer = serde_html_form::Deserializer::new(form_urlencoded::parse(&bytes));

let value = serde_path_to_error::deserialize::<_, T>(deserializer)
.map_err(FailedToDeserializeForm::from_err)?;

Ok(Self(value))
serde_path_to_error::deserialize::<_, T>(deserializer)
.map(Self)
.map_err(|err| {
if is_get_or_head {
FailedToDeserializeForm::from_err(err).into()
} else {
FailedToDeserializeFormBody::from_err(err).into()
}
})
}
}

Expand All @@ -70,13 +78,22 @@ define_rejection! {
pub struct FailedToDeserializeForm(Error);
}

define_rejection! {
#[status = UNPROCESSABLE_ENTITY]
#[body = "Failed to deserialize form body"]
/// Rejection type used if the [`Form`](Form) extractor is unable to
/// deserialize the form body into the target type.
pub struct FailedToDeserializeFormBody(Error);
}

composite_rejection! {
/// Rejection used for [`Form`].
///
/// Contains one variant for each way the [`Form`] extractor can fail.
pub enum FormRejection {
RawFormRejection,
FailedToDeserializeForm,
FailedToDeserializeFormBody,
}
}

Expand Down Expand Up @@ -146,10 +163,10 @@ mod tests {
.header(CONTENT_TYPE, APPLICATION_WWW_FORM_URLENCODED.as_ref())
.body("a=false")
.await;
assert_eq!(res.status(), StatusCode::BAD_REQUEST);
assert_eq!(res.status(), StatusCode::UNPROCESSABLE_ENTITY);
assert_eq!(
res.text().await,
"Failed to deserialize form: a: invalid digit found in string"
"Failed to deserialize form body: a: invalid digit found in string"
);
}
}

0 comments on commit 09841ff

Please sign in to comment.