Crates.io | trace-err |
lib.rs | trace-err |
version | 0.1.1 |
source | src |
created_at | 2024-06-12 10:51:06.021998 |
updated_at | 2024-10-07 09:20:59.176469 |
description | A small extension to the tracing crate, which provides a single method 'trace_expect()' for Result |
homepage | |
repository | https://github.com/ricktaylor/trace-err |
max_upload_size | |
id | 1269531 |
size | 8,383 |
A small extension to the tracing crate, which provides a single method for core::result::Result<T, E>
and core::option::Option<T>
.
Adds trace_expect
to Result
, which invoke the tracing::error!
macro (in case of Result::Err
) in addition to unwrapping/expecting the Result
.
Adds trace_expect
to Option
, which invoke the tracing::error!
macro (in case of Option::None
) in addition to unwrapping/expecting the Option
.
Shamelessly derived from the Log_Err crate.
Shorthand for:
use tracing::error;
fn something() -> Result<(), &'static str> {Err("there was some problem")}
let msg = "Some message";
something().map_err(|e| tracing::error!("{}: {:?}", msg, e)).expect(msg)
Example:
use std::fs::File;
use trace_err::*;
let mut file = File::open("foo.txt").trace_expect("Error creating file");
# Error will be traced with the error! macro
2024-06-12T09:31:23.933299Z ERROR expect: trace-err/lib.rs:87:39: Error creating file: Os { code: 2, kind: NotFound, message: "No such file or directory" }
# Main program panic'ing with same message
thread 'main' panicked at trace-err/lib.rs:87:39:
Error creating file: Os { code: 2, kind: NotFound, message: "No such file or directory" }