From 19f40956f0b3febacc496e0bf473f121af71c7f6 Mon Sep 17 00:00:00 2001 From: Jan Trefil <8711792+htrefil@users.noreply.github.com> Date: Mon, 10 Jun 2024 20:25:43 +0200 Subject: [PATCH 1/3] Add status methods to Form and Query rejections --- axum-extra/src/extract/form.rs | 10 ++++++++++ axum-extra/src/extract/query.rs | 11 ++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/axum-extra/src/extract/form.rs b/axum-extra/src/extract/form.rs index d7ee6f9842..e426cdb59e 100644 --- a/axum-extra/src/extract/form.rs +++ b/axum-extra/src/extract/form.rs @@ -77,6 +77,16 @@ pub enum FormRejection { FailedToDeserializeForm(Error), } +impl FormRejection { + 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 304d72b87b..24056a769f 100644 --- a/axum-extra/src/extract/query.rs +++ b/axum-extra/src/extract/query.rs @@ -84,11 +84,20 @@ pub enum QueryRejection { FailedToDeserializeQueryString(Error), } +#[cfg(feature = "query")] +impl QueryRejection { + 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 { Self::FailedToDeserializeQueryString(inner) => ( - StatusCode::BAD_REQUEST, + self.status(), format!("Failed to deserialize query string: {}", inner), ) .into_response(), From a9fd724418b4800d1c118690d0190a5f45ee4d6a Mon Sep 17 00:00:00 2001 From: Jan Trefil <8711792+htrefil@users.noreply.github.com> Date: Mon, 10 Jun 2024 20:26:50 +0200 Subject: [PATCH 2/3] Add doc comments --- axum-extra/src/extract/form.rs | 1 + axum-extra/src/extract/query.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/axum-extra/src/extract/form.rs b/axum-extra/src/extract/form.rs index e426cdb59e..328903fc80 100644 --- a/axum-extra/src/extract/form.rs +++ b/axum-extra/src/extract/form.rs @@ -78,6 +78,7 @@ pub enum FormRejection { } 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 { diff --git a/axum-extra/src/extract/query.rs b/axum-extra/src/extract/query.rs index 24056a769f..7ec2b1b47d 100644 --- a/axum-extra/src/extract/query.rs +++ b/axum-extra/src/extract/query.rs @@ -86,6 +86,7 @@ pub enum QueryRejection { #[cfg(feature = "query")] impl QueryRejection { + /// Get the status code used for this rejection. pub fn status(&self) -> http::StatusCode { match self { Self::FailedToDeserializeQueryString(_) => http::StatusCode::BAD_REQUEST, From 99cfd7830bf301ab786ccfd863b7ac7645cb976a Mon Sep 17 00:00:00 2001 From: Jan Trefil <8711792+htrefil@users.noreply.github.com> Date: Mon, 10 Jun 2024 20:41:23 +0200 Subject: [PATCH 3/3] Remove cfg --- axum-extra/src/extract/query.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/axum-extra/src/extract/query.rs b/axum-extra/src/extract/query.rs index 24b5cdd851..4393f2c012 100644 --- a/axum-extra/src/extract/query.rs +++ b/axum-extra/src/extract/query.rs @@ -111,7 +111,6 @@ pub enum QueryRejection { FailedToDeserializeQueryString(Error), } -#[cfg(feature = "query")] impl QueryRejection { /// Get the status code used for this rejection. pub fn status(&self) -> http::StatusCode {