diff --git a/axum-extra/src/extract/form.rs b/axum-extra/src/extract/form.rs index 8d2d30f91c..d922686705 100644 --- a/axum-extra/src/extract/form.rs +++ b/axum-extra/src/extract/form.rs @@ -77,6 +77,17 @@ pub enum FormRejection { FailedToDeserializeForm(Error), } +impl FormRejection { + /// Get the status code used for this rejection. + pub fn status(&self) -> http::StatusCode { + // Make sure to keep this in sync with `IntoResponse` impl. + match self { + Self::RawFormRejection(inner) => inner.status(), + Self::FailedToDeserializeForm(_) => http::StatusCode::BAD_REQUEST, + } + } +} + impl IntoResponse for FormRejection { fn into_response(self) -> Response { match self { diff --git a/axum-extra/src/extract/query.rs b/axum-extra/src/extract/query.rs index 7bd023cf06..24c7429824 100644 --- a/axum-extra/src/extract/query.rs +++ b/axum-extra/src/extract/query.rs @@ -111,6 +111,15 @@ pub enum QueryRejection { FailedToDeserializeQueryString(Error), } +impl QueryRejection { + /// Get the status code used for this rejection. + pub fn status(&self) -> http::StatusCode { + match self { + Self::FailedToDeserializeQueryString(_) => http::StatusCode::BAD_REQUEST, + } + } +} + impl IntoResponse for QueryRejection { fn into_response(self) -> Response { match self { @@ -239,7 +248,7 @@ impl IntoResponse for OptionalQueryRejection { fn into_response(self) -> Response { match self { Self::FailedToDeserializeQueryString(inner) => ( - StatusCode::BAD_REQUEST, + self.status(), format!("Failed to deserialize query string: {inner}"), ) .into_response(),