From bdd5244ac5691c8da56ea904dc74735c8fc98b98 Mon Sep 17 00:00:00 2001 From: Justin Karneges Date: Mon, 13 May 2024 14:29:39 -0700 Subject: [PATCH] improve logical error logging --- src/connection.rs | 60 ++++++++++++++++++++++------------------------- 1 file changed, 28 insertions(+), 32 deletions(-) diff --git a/src/connection.rs b/src/connection.rs index 9d4688b8..1c4fb8da 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -361,6 +361,20 @@ enum Error { } impl Error { + // returns true if the error represents a logic error (a bug in the code) + // that could warrant a panic or high severity log level + fn is_logical(&self) -> bool { + matches!(self, Error::ValueActive) + } + + fn log_level(&self) -> Level { + if self.is_logical() { + Level::Error + } else { + Level::Debug + } + } + fn to_condition(&self) -> &'static str { match self { Error::Io(e) if e.kind() == io::ErrorKind::ConnectionRefused => { @@ -2371,14 +2385,7 @@ pub async fn server_req_connection debug!("server-conn {}: finished", cid), - Err(e) => { - let level = match e { - Error::ValueActive => Level::Error, - _ => Level::Debug, - }; - - log!(level, "server-conn {}: process error: {:?}", cid, e); - } + Err(e) => log!(e.log_level(), "server-conn {}: process error: {:?}", cid, e), } } @@ -4452,14 +4459,7 @@ pub async fn server_stream_connection debug!("server-conn {}: finished", cid), - Err(e) => { - let level = match e { - Error::ValueActive => Level::Error, - _ => Level::Debug, - }; - - log!(level, "server-conn {}: process error: {:?}", cid, e); - } + Err(e) => log!(e.log_level(), "server-conn {}: process error: {:?}", cid, e), } } @@ -5194,14 +5194,12 @@ pub async fn client_req_connection( .await { Ok(()) => debug!("client-conn {}: finished", log_id), - Err(e) => { - let level = match e { - Error::ValueActive => Level::Error, - _ => Level::Debug, - }; - - log!(level, "client-conn {}: process error: {:?}", log_id, e); - } + Err(e) => log!( + e.log_level(), + "client-conn {}: process error: {:?}", + log_id, + e + ), } } @@ -6185,14 +6183,12 @@ pub async fn client_stream_connection( .await { Ok(()) => debug!("client-conn {}: finished", log_id), - Err(e) => { - let level = match e { - Error::ValueActive => Level::Error, - _ => Level::Debug, - }; - - log!(level, "client-conn {}: process error: {:?}", log_id, e); - } + Err(e) => log!( + e.log_level(), + "client-conn {}: process error: {:?}", + log_id, + e + ), } }