Log built-in rejections (#1890)

This commit is contained in:
David Pedersen
2023-04-11 16:57:49 +02:00
committed by GitHub
parent 2c2cf361dd
commit 6b106f4bab
18 changed files with 329 additions and 192 deletions
+3
View File
@@ -1,5 +1,8 @@
//! Rejection response types.
use crate::__composite_rejection as composite_rejection;
use crate::__define_rejection as define_rejection;
use crate::BoxError;
composite_rejection! {
+94 -12
View File
@@ -1,4 +1,79 @@
macro_rules! define_rejection {
/// Private API.
#[doc(hidden)]
#[macro_export]
macro_rules! __log_rejection {
(
rejection_type = $ty:ident,
body_text = $body_text:expr,
status = $status:expr,
) => {
#[cfg(feature = "tracing")]
{
tracing::event!(
target: "axum::rejection",
tracing::Level::TRACE,
status = $status.as_u16(),
body = $body_text,
rejection_type = std::any::type_name::<$ty>(),
"rejecting request",
);
}
};
}
/// Private API.
#[doc(hidden)]
#[macro_export]
macro_rules! __define_rejection {
(
#[status = $status:ident]
#[body = $body:expr]
$(#[$m:meta])*
pub struct $name:ident;
) => {
$(#[$m])*
#[derive(Debug)]
#[non_exhaustive]
pub struct $name;
impl $crate::response::IntoResponse for $name {
fn into_response(self) -> $crate::response::Response {
$crate::__log_rejection!(
rejection_type = $name,
body_text = $body,
status = http::StatusCode::$status,
);
(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
}
}
impl std::fmt::Display for $name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", $body)
}
}
impl std::error::Error for $name {}
impl Default for $name {
fn default() -> Self {
Self
}
}
};
(
#[status = $status:ident]
#[body = $body:expr]
@@ -7,15 +82,25 @@ macro_rules! define_rejection {
) => {
$(#[$m])*
#[derive(Debug)]
pub struct $name(pub(crate) crate::Error);
pub struct $name(pub(crate) $crate::Error);
impl $name {
#[allow(dead_code)]
pub(crate) fn from_err<E>(err: E) -> Self
where
E: Into<crate::BoxError>,
E: Into<$crate::BoxError>,
{
Self(crate::Error::new(err))
Self($crate::Error::new(err))
}
}
impl $crate::response::IntoResponse for $name {
fn into_response(self) -> $crate::response::Response {
$crate::__log_rejection!(
rejection_type = $name,
body_text = self.body_text(),
status = http::StatusCode::$status,
);
(self.status(), self.body_text()).into_response()
}
}
@@ -31,12 +116,6 @@ macro_rules! define_rejection {
}
}
impl crate::response::IntoResponse for $name {
fn into_response(self) -> $crate::response::Response {
(self.status(), self.body_text()).into_response()
}
}
impl std::fmt::Display for $name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", $body)
@@ -51,7 +130,10 @@ macro_rules! define_rejection {
};
}
macro_rules! composite_rejection {
/// Private API.
#[doc(hidden)]
#[macro_export]
macro_rules! __composite_rejection {
(
$(#[$m:meta])*
pub enum $name:ident {