Skip to content

Commit

Permalink
Add status and body_text methods to built-in rejections (#1612)
Browse files Browse the repository at this point in the history
* 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 #1611

* changelog
  • Loading branch information
davidpdrsn committed Dec 2, 2022
1 parent 5826ca2 commit 7e13d69
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 22 deletions.
4 changes: 3 additions & 1 deletion axum-core/CHANGELOG.md
Expand Up @@ -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)

Expand Down
37 changes: 33 additions & 4 deletions axum-core/src/macros.rs
Expand Up @@ -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()
}
}

Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion axum/CHANGELOG.md
Expand Up @@ -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)

Expand Down
37 changes: 26 additions & 11 deletions axum/src/extract/path/mod.rs
Expand Up @@ -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()
}
}

Expand Down
51 changes: 46 additions & 5 deletions axum/src/macros.rs
Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 7e13d69

Please sign in to comment.