Rework Form and Query rejections (#1496)

* Change `FailedToDeserializeQueryString` rejection for `Form`

Its now called `FailedToDeserializeForm`.

* changelog

* Make dedicate rejection type for axum-extra's `Form`

* update trybuild test

* Make dedicate rejection type for axum-extra's `Query`
This commit is contained in:
David Pedersen
2022-11-08 20:31:06 +00:00
committed by GitHub
parent 8d6313afa0
commit e0ef641e5f
8 changed files with 119 additions and 82 deletions
+3
View File
@@ -47,6 +47,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- **added:** `FromRequest` and `FromRequestParts` derive macro re-exports from
[`axum-macros`] behind the `macros` feature ([#1352])
- **added:** Add `extract::RawForm` for accessing raw urlencoded query bytes or request body ([#1487])
- **breaking:** Rename `FormRejection::FailedToDeserializeQueryString` to
`FormRejection::FailedToDeserializeForm` ([#1496])
[#1352]: https://github.com/tokio-rs/axum/pull/1352
[#1368]: https://github.com/tokio-rs/axum/pull/1368
@@ -63,6 +65,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[#1420]: https://github.com/tokio-rs/axum/pull/1420
[#1421]: https://github.com/tokio-rs/axum/pull/1421
[#1487]: https://github.com/tokio-rs/axum/pull/1487
[#1496]: https://github.com/tokio-rs/axum/pull/1496
# 0.6.0-rc.2 (10. September, 2022)
+2 -2
View File
@@ -59,8 +59,8 @@ where
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
let query = parts.uri.query().unwrap_or_default();
let value = serde_urlencoded::from_str(query)
.map_err(FailedToDeserializeQueryString::__private_new)?;
let value =
serde_urlencoded::from_str(query).map_err(FailedToDeserializeQueryString::from_err)?;
Ok(Query(value))
}
}
+13 -33
View File
@@ -1,8 +1,5 @@
//! Rejection response types.
use crate::{BoxError, Error};
use axum_core::response::{IntoResponse, Response};
pub use crate::extract::path::FailedToDeserializePathParams;
pub use axum_core::extract::rejection::*;
@@ -73,39 +70,22 @@ define_rejection! {
pub struct FailedToResolveHost;
}
/// Rejection type for extractors that deserialize query strings if the input
/// couldn't be deserialized into the target type.
#[derive(Debug)]
pub struct FailedToDeserializeQueryString {
error: Error,
define_rejection! {
#[status = BAD_REQUEST]
#[body = "Failed to deserialize form"]
/// Rejection type used if the [`Form`](super::Form) extractor is unable to
/// deserialize the form into the target type.
pub struct FailedToDeserializeForm(Error);
}
impl FailedToDeserializeQueryString {
#[doc(hidden)]
pub fn __private_new<E>(error: E) -> Self
where
E: Into<BoxError>,
{
FailedToDeserializeQueryString {
error: Error::new(error),
}
}
define_rejection! {
#[status = BAD_REQUEST]
#[body = "Failed to deserialize query string"]
/// Rejection type used if the [`Query`](super::Query) extractor is unable to
/// deserialize the form into the target type.
pub struct FailedToDeserializeQueryString(Error);
}
impl IntoResponse for FailedToDeserializeQueryString {
fn into_response(self) -> Response {
(http::StatusCode::BAD_REQUEST, self.to_string()).into_response()
}
}
impl std::fmt::Display for FailedToDeserializeQueryString {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Failed to deserialize query string: {}", self.error,)
}
}
impl std::error::Error for FailedToDeserializeQueryString {}
composite_rejection! {
/// Rejection used for [`Query`](super::Query).
///
@@ -123,7 +103,7 @@ composite_rejection! {
/// can fail.
pub enum FormRejection {
InvalidFormContentType,
FailedToDeserializeQueryString,
FailedToDeserializeForm,
BytesRejection,
}
}
+1 -1
View File
@@ -76,7 +76,7 @@ where
match req.extract().await {
Ok(RawForm(bytes)) => {
let value = serde_urlencoded::from_bytes(&bytes)
.map_err(FailedToDeserializeQueryString::__private_new)?;
.map_err(FailedToDeserializeForm::from_err)?;
Ok(Form(value))
}
Err(RawFormRejection::BytesRejection(r)) => Err(FormRejection::BytesRejection(r)),