mirror of
https://github.com/tokio-rs/axum.git
synced 2026-08-27 00:00:24 +02:00
Add Form extractor (#12)
Fixes https://github.com/davidpdrsn/tower-web/issues/2
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
//! Rejection response types.
|
||||
|
||||
use tower::BoxError;
|
||||
|
||||
use super::IntoResponse;
|
||||
use crate::body::Body;
|
||||
|
||||
@@ -147,6 +149,13 @@ define_rejection! {
|
||||
pub struct RequestAlreadyExtracted;
|
||||
}
|
||||
|
||||
define_rejection! {
|
||||
#[status = BAD_REQUEST]
|
||||
#[body = "Form requests must have `Content-Type: x-www-form-urlencoded`"]
|
||||
/// Rejection type used if you try and extract the request more than once.
|
||||
pub struct InvalidFormContentType;
|
||||
}
|
||||
|
||||
/// Rejection type for [`UrlParams`](super::UrlParams) if the capture route
|
||||
/// param didn't have the expected type.
|
||||
#[derive(Debug)]
|
||||
@@ -172,3 +181,34 @@ impl IntoResponse for InvalidUrlParam {
|
||||
res
|
||||
}
|
||||
}
|
||||
|
||||
/// 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: BoxError,
|
||||
type_name: &'static str,
|
||||
}
|
||||
|
||||
impl FailedToDeserializeQueryString {
|
||||
pub(super) fn new<T, E>(error: E) -> Self
|
||||
where
|
||||
E: Into<BoxError>,
|
||||
{
|
||||
FailedToDeserializeQueryString {
|
||||
error: error.into(),
|
||||
type_name: std::any::type_name::<T>(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoResponse for FailedToDeserializeQueryString {
|
||||
fn into_response(self) -> http::Response<Body> {
|
||||
let mut res = http::Response::new(Body::from(format!(
|
||||
"Failed to deserialize query string. Expected something of type `{}`. Error: {}",
|
||||
self.type_name, self.error,
|
||||
)));
|
||||
*res.status_mut() = http::StatusCode::BAD_REQUEST;
|
||||
res
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user