Skip to content

Commit 0b06f0c

Browse files
authored
Allow querying of HTTP status codes for query and form rejections (#2781)
1 parent 12d7d9c commit 0b06f0c

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

axum-extra/src/extract/form.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,24 @@ pub enum FormRejection {
7777
FailedToDeserializeForm(Error),
7878
}
7979

80+
impl FormRejection {
81+
/// Get the status code used for this rejection.
82+
pub fn status(&self) -> StatusCode {
83+
// Make sure to keep this in sync with `IntoResponse` impl.
84+
match self {
85+
Self::RawFormRejection(inner) => inner.status(),
86+
Self::FailedToDeserializeForm(_) => StatusCode::BAD_REQUEST,
87+
}
88+
}
89+
}
90+
8091
impl IntoResponse for FormRejection {
8192
fn into_response(self) -> Response {
93+
let status = self.status();
8294
match self {
8395
Self::RawFormRejection(inner) => inner.into_response(),
8496
Self::FailedToDeserializeForm(inner) => {
8597
let body = format!("Failed to deserialize form: {inner}");
86-
let status = StatusCode::BAD_REQUEST;
8798
axum_core::__log_rejection!(
8899
rejection_type = Self,
89100
body_text = body,

axum-extra/src/extract/query.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,21 @@ pub enum QueryRejection {
111111
FailedToDeserializeQueryString(Error),
112112
}
113113

114+
impl QueryRejection {
115+
/// Get the status code used for this rejection.
116+
pub fn status(&self) -> StatusCode {
117+
match self {
118+
Self::FailedToDeserializeQueryString(_) => StatusCode::BAD_REQUEST,
119+
}
120+
}
121+
}
122+
114123
impl IntoResponse for QueryRejection {
115124
fn into_response(self) -> Response {
125+
let status = self.status();
116126
match self {
117127
Self::FailedToDeserializeQueryString(inner) => {
118128
let body = format!("Failed to deserialize query string: {inner}");
119-
let status = StatusCode::BAD_REQUEST;
120129
axum_core::__log_rejection!(
121130
rejection_type = Self,
122131
body_text = body,

0 commit comments

Comments
 (0)