Skip to content

Improve panic logging by formatting panic payload for clarity #218

@sourcery-ai

Description

@sourcery-ai

Currently, when logging a panic object, the code logs the panic payload directly. This can result in unclear or unhelpful log output, especially if the panic payload is not a string. To improve the clarity of panic logs, we should convert the panic payload to a string or format it appropriately before logging.

Suggested Fix:
Update the panic logging code to attempt to downcast the panic payload to a &str or String, and fall back to formatting it with Debug if those fail. Here is a code snippet illustrating the proposed change:

let panic_msg = if let Some(s) = panic.downcast_ref::<&str>() {
    s.to_string()
} else if let Some(s) = panic.downcast_ref::<String>() {
    s.clone()
} else {
    format!("{:?}", panic)
};
tracing::error!(
    panic = %panic_msg,
    ?peer_addr,
    "connection task panicked"
);

Action Items:

  • Refactor the panic logging code as shown above.
  • Ensure that all panic logs are clear and provide useful information for debugging.
  • Add or update tests if necessary to verify the improved logging behavior.

Issue: #218


I created this issue for @leynos from #215 (comment).

Tips and commands

Getting Help

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions