4 releases

0.8.5 Dec 28, 2024
0.8.4 Dec 28, 2024
0.8.3 Dec 23, 2024
0.0.1 Dec 6, 2024

#283 in Debugging

Download history 82/week @ 2024-12-02 28/week @ 2024-12-09 389/week @ 2024-12-23 211/week @ 2024-12-30 237/week @ 2025-01-06 305/week @ 2025-01-13

1,142 downloads per month
Used in 2 crates (via error_set)

Apache-2.0

33KB
692 lines

err_trail

Convience methods on Result and Option for logging when an Err or None is ecountered. Similar to anyhow but for logging.

Feature Flags

tracing / log / defmt : Enables support for the tracing or log or defmt crates. Methods are added to Result and are executed when the Result is an Err for logging purposes. They work similarly to anyhow's .context(..) method. e.g.

let result: Result<(), &str> = Err("operation failed");

let value: Result<(), &str> = result.error_context("If `Err`, this message is logged as error via tracing/log/defmt");
let value: Result<(), &str> = result.warn_context("If `Err`, this message is logged as warn via tracing/log/defmt");
let value: Result<(), &str> = result.with_error_context(|err| format!("If `Err`, this message is logged as error via tracing/log/defmt: {}", err));
let value: Option<()> = result.consume_as_error(); // If `Err`, the `Err` is logged as error via tracing/log/defmt
let value: Option<()> = result.consume_with_warn(|err| format!("If `Err`, this message is logged as warn via tracing/log/defmt: {}", err));
// ...etc.

This is useful tracing context around errors. e.g.

let val = func().warn_context("`func` failed, here is some extra context like variable values")?;
let val = func().consume_as_warn();

rather than

let val = func().inspect_err(|err| tracing::warn!("`func` failed, here is some extra context like variable values"))?;
let val = func().inspect_err(|err| tracing::warn!("{}", err)).ok();

Note: a stub feature flag also exists to be used by libraries. This allows the api's to be used in libraries while a downstream binary can ultimately decide the implementation. If no implementations is selected, since all the above methods are inlined, the code will be optimized away during compilation.

Guide

warn - Represents a bad state in which the current process can continue.

error - Represents a bad state in which the current process cannot continue.

  • If returning a Result to the calling function, context should usually be warn
  • If consuming a Result, context should usually be error

Dependencies

~0–265KB