Skip to content

Commit

Permalink
Add status methods to Form and Query rejections
Browse files Browse the repository at this point in the history
  • Loading branch information
htrefil authored and Turbo87 committed Dec 26, 2024
1 parent 12d7d9c commit 0164311
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
11 changes: 11 additions & 0 deletions axum-extra/src/extract/form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 10 additions & 1 deletion axum-extra/src/extract/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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(),
Expand Down

0 comments on commit 0164311

Please sign in to comment.