mirror of
https://github.com/tokio-rs/axum.git
synced 2026-08-28 00:00:20 +02:00
Add status and body_text methods to built-in rejections (#1612)
* Add `status` and `body_text` methods to built-in rejections This should make it easier to customize a built-in rejection while preserving either the status or body. Fixes https://github.com/tokio-rs/axum/issues/1611 * changelog
This commit is contained in:
+33
-4
@@ -19,12 +19,21 @@ macro_rules! define_rejection {
|
||||
}
|
||||
}
|
||||
|
||||
impl $name {
|
||||
/// Get the response body text used for this rejection.
|
||||
pub fn body_text(&self) -> String {
|
||||
format!(concat!($body, ": {}"), self.0).into()
|
||||
}
|
||||
|
||||
/// Get the status code used for this rejection.
|
||||
pub fn status(&self) -> http::StatusCode {
|
||||
http::StatusCode::$status
|
||||
}
|
||||
}
|
||||
|
||||
impl crate::response::IntoResponse for $name {
|
||||
fn into_response(self) -> $crate::response::Response {
|
||||
(
|
||||
http::StatusCode::$status,
|
||||
format!(concat!($body, ": {}"), self.0)
|
||||
).into_response()
|
||||
(self.status(), self.body_text()).into_response()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,6 +79,26 @@ macro_rules! composite_rejection {
|
||||
}
|
||||
}
|
||||
|
||||
impl $name {
|
||||
/// Get the response body text used for this rejection.
|
||||
pub fn body_text(&self) -> String {
|
||||
match self {
|
||||
$(
|
||||
Self::$variant(inner) => inner.body_text(),
|
||||
)+
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the status code used for this rejection.
|
||||
pub fn status(&self) -> http::StatusCode {
|
||||
match self {
|
||||
$(
|
||||
Self::$variant(inner) => inner.status(),
|
||||
)+
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$(
|
||||
impl From<$variant> for $name {
|
||||
fn from(inner: $variant) -> Self {
|
||||
|
||||
Reference in New Issue
Block a user