mirror of
https://github.com/tokio-rs/axum.git
synced 2024-11-22 15:17:18 +01: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:
parent
5826ca268c
commit
7e13d69639
5 changed files with 111 additions and 22 deletions
|
@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||
|
||||
# Unreleased
|
||||
|
||||
- None.
|
||||
- **added:** Add `body_text` and `status` methods to built-in rejections ([#1612])
|
||||
|
||||
[#1612]: https://github.com/tokio-rs/axum/pull/1612
|
||||
|
||||
# 0.3.0 (25. November, 2022)
|
||||
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||
|
||||
# Unreleased
|
||||
|
||||
- None.
|
||||
- **added:** Add `body_text` and `status` methods to built-in rejections ([#1612])
|
||||
|
||||
[#1612]: https://github.com/tokio-rs/axum/pull/1612
|
||||
|
||||
# 0.6.1 (29. November, 2022)
|
||||
|
||||
|
|
|
@ -383,24 +383,39 @@ impl FailedToDeserializePathParams {
|
|||
pub fn into_kind(self) -> ErrorKind {
|
||||
self.0.kind
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoResponse for FailedToDeserializePathParams {
|
||||
fn into_response(self) -> Response {
|
||||
let (status, body) = match self.0.kind {
|
||||
/// Get the response body text used for this rejection.
|
||||
pub fn body_text(&self) -> String {
|
||||
match self.0.kind {
|
||||
ErrorKind::Message(_)
|
||||
| ErrorKind::InvalidUtf8InPathParam { .. }
|
||||
| ErrorKind::ParseError { .. }
|
||||
| ErrorKind::ParseErrorAtIndex { .. }
|
||||
| ErrorKind::ParseErrorAtKey { .. } => (
|
||||
StatusCode::BAD_REQUEST,
|
||||
format!("Invalid URL: {}", self.0.kind),
|
||||
),
|
||||
| ErrorKind::ParseErrorAtKey { .. } => format!("Invalid URL: {}", self.0.kind),
|
||||
ErrorKind::WrongNumberOfParameters { .. } | ErrorKind::UnsupportedType { .. } => {
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, self.0.kind.to_string())
|
||||
self.0.kind.to_string()
|
||||
}
|
||||
};
|
||||
(status, body).into_response()
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the status code used for this rejection.
|
||||
pub fn status(&self) -> StatusCode {
|
||||
match self.0.kind {
|
||||
ErrorKind::Message(_)
|
||||
| ErrorKind::InvalidUtf8InPathParam { .. }
|
||||
| ErrorKind::ParseError { .. }
|
||||
| ErrorKind::ParseErrorAtIndex { .. }
|
||||
| ErrorKind::ParseErrorAtKey { .. } => StatusCode::BAD_REQUEST,
|
||||
ErrorKind::WrongNumberOfParameters { .. } | ErrorKind::UnsupportedType { .. } => {
|
||||
StatusCode::INTERNAL_SERVER_ERROR
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoResponse for FailedToDeserializePathParams {
|
||||
fn into_response(self) -> Response {
|
||||
(self.status(), self.body_text()).into_response()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -59,7 +59,19 @@ macro_rules! define_rejection {
|
|||
|
||||
impl $crate::response::IntoResponse for $name {
|
||||
fn into_response(self) -> $crate::response::Response {
|
||||
(http::StatusCode::$status, $body).into_response()
|
||||
(self.status(), $body).into_response()
|
||||
}
|
||||
}
|
||||
|
||||
impl $name {
|
||||
/// Get the response body text used for this rejection.
|
||||
pub fn body_text(&self) -> String {
|
||||
$body.into()
|
||||
}
|
||||
|
||||
/// Get the status code used for this rejection.
|
||||
pub fn status(&self) -> http::StatusCode {
|
||||
http::StatusCode::$status
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -99,10 +111,19 @@ macro_rules! define_rejection {
|
|||
|
||||
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()
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -148,6 +169,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 {
|
||||
|
|
Loading…
Reference in a new issue