Skip to content

Commit

Permalink
improve logical error logging (#47987)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkarneges authored May 13, 2024
1 parent 1f4013e commit 9f925f6
Showing 1 changed file with 28 additions and 32 deletions.
60 changes: 28 additions & 32 deletions src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -2371,14 +2385,7 @@ pub async fn server_req_connection<P: CidProvider, S: AsyncRead + AsyncWrite + I
.await
{
Ok(()) => 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),
}
}

Expand Down Expand Up @@ -4452,14 +4459,7 @@ pub async fn server_stream_connection<P: CidProvider, S: AsyncRead + AsyncWrite
.await
{
Ok(()) => 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),
}
}

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

Expand Down Expand Up @@ -6185,14 +6183,12 @@ pub async fn client_stream_connection<E>(
.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
),
}
}

Expand Down

0 comments on commit 9f925f6

Please sign in to comment.