From f10f5319db5b0189901008487f0b9b5b4b375e58 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Wed, 10 Jul 2024 06:34:03 +0000 Subject: [PATCH] elaborate error log functor stack Signed-off-by: Jason Volk --- src/core/error.rs | 70 ++++++++++++++++++++++++++++++------ src/service/admin/console.rs | 5 ++- 2 files changed, 63 insertions(+), 12 deletions(-) diff --git a/src/core/error.rs b/src/core/error.rs index 1959081a..7069d1e6 100644 --- a/src/core/error.rs +++ b/src/core/error.rs @@ -117,25 +117,73 @@ impl Error { } #[inline] -pub fn log(e: &Error) { - error!(?e); +pub fn else_log(error: E) -> Result +where + T: Default, + Error: From, +{ + Ok(default_log(error)) } #[inline] -pub fn debug_log(e: &Error) { - debug_error!(?e); +pub fn else_debug_log(error: E) -> Result +where + T: Default, + Error: From, +{ + Ok(default_debug_log(error)) } #[inline] -pub fn into_log(e: Error) { - error!(?e); - drop(e); +pub fn default_log(error: E) -> T +where + T: Default, + Error: From, +{ + let error = Error::from(error); + inspect_log(&error); + T::default() } #[inline] -pub fn into_debug_log(e: Error) { - debug_error!(?e); - drop(e); +pub fn default_debug_log(error: E) -> T +where + T: Default, + Error: From, +{ + let error = Error::from(error); + inspect_debug_log(&error); + T::default() +} + +#[inline] +pub fn map_log(error: E) -> Error +where + Error: From, +{ + let error = Error::from(error); + inspect_log(&error); + error +} + +#[inline] +pub fn map_debug_log(error: E) -> Error +where + Error: From, +{ + let error = Error::from(error); + inspect_debug_log(&error); + error +} + +#[inline] +pub fn inspect_log(error: &E) { + error!("{error}"); +} + +#[inline] +pub fn inspect_debug_log(error: &E) { + debug_error!("{error:?}"); } impl fmt::Debug for Error { @@ -151,7 +199,7 @@ impl axum::response::IntoResponse for Error { let response: UiaaResponse = self.into(); response .try_into_http_response::() - .inspect_err(|e| error!(?e)) + .inspect_err(|e| error!("error response error: {e}")) .map_or_else( |_| StatusCode::INTERNAL_SERVER_ERROR.into_response(), |r| r.map(BytesMut::freeze).map(Full::new).into_response(), diff --git a/src/service/admin/console.rs b/src/service/admin/console.rs index 2f66b1d5..c590b928 100644 --- a/src/service/admin/console.rs +++ b/src/service/admin/console.rs @@ -95,7 +95,10 @@ impl Console { ReadlineEvent::Line(string) => self.clone().handle(string).await, ReadlineEvent::Interrupted => continue, ReadlineEvent::Eof => break, - ReadlineEvent::Quit => services().server.shutdown().unwrap_or_else(error::into_log), + ReadlineEvent::Quit => services() + .server + .shutdown() + .unwrap_or_else(error::default_log), }, Err(error) => match error { ReadlineError::Closed => break,