Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Allow querying of HTTP status codes for query and form rejections #2781

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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(),
Expand Down
Loading