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

Add status and body_text methods to built-in rejections #1612

Merged
merged 2 commits into from Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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