diff --git a/axum-extra/src/extract/form.rs b/axum-extra/src/extract/form.rs index 453e782372..c140723734 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 7822f108a2..4393f2c012 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 { @@ -238,7 +247,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(),