Trait zellij_utils::errors::LoggableError

source ·
pub trait LoggableError<T>: Sized {
    // Required method
    fn print_error<F: Fn(&str)>(self, fun: F) -> Self;

    // Provided methods
    fn to_log(self) -> Self { ... }
    fn to_stderr(self) -> Self { ... }
    fn to_stdout(self) -> Self { ... }
}
Expand description

Helper trait to easily log error types.

The print_error function takes a closure which takes a &str and fares with it as necessary to log the error to some usable location. For convenience, logging to stdout, stderr and log::error! is already implemented.

Note that the trait functions pass the error through unmodified, so they can be chained with the usual handling of std::result::Result types.

Required Methods§

source

fn print_error<F: Fn(&str)>(self, fun: F) -> Self

Gives a formatted error message derived from self to the closure fun for printing/logging as appropriate.

§Examples
use anyhow;
use zellij_utils::errors::LoggableError;

let my_err: anyhow::Result<&str> = Err(anyhow::anyhow!("Test error"));
my_err
    .print_error(|msg| println!("{msg}"))
    .unwrap();

Provided Methods§

source

fn to_log(self) -> Self

Convenienve function, calls print_error and logs the result as error.

This is not a wrapper around log::error!, because the log crate uses a lot of compile time macros from std to determine caller locations/module names etc. Since these are resolved at compile time in the location they are written, they would always resolve to the location in this function where log::error! is called, masking the real caller location. Hence, we build the log message ourselves. This means that we lose the information about the calling module (Because it can only be resolved at compile time), however the callers file and line number are preserved.

source

fn to_stderr(self) -> Self

Convenienve function, calls print_error with the closure |msg| eprintln!("{}", msg).

source

fn to_stdout(self) -> Self

Convenienve function, calls print_error with the closure |msg| println!("{}", msg).

Object Safety§

This trait is not object safe.

Implementors§

source§

impl<T> LoggableError<T> for Result<T>