#snafu #error #visibility #pub #quick-tracing #snafu-tracing #debug-trace

snafu-tracing

Snafu tracing is an error handling mechanism with pseudo-stack traces implemented through SNAFU, the proc macro trace_error, and the DebugTrace trait, inspired by GreptimeDB

1 unstable release

Uses new Rust 2024

0.8.3 Apr 21, 2025
0.8.2 Apr 21, 2025
0.8.1 Apr 21, 2025
0.8.0 Apr 21, 2025

#5 in #pub

Download history 229/week @ 2025-04-20

229 downloads per month

MIT/Apache

6KB

snafu-tracing

Snafu tracing is an error handling mechanism with pseudo-stack traces implemented through SNAFU, the proc macro trace_error, and the DebugTrace trait, inspired by GreptimeDB.

Example

use snafu::Snafu;
use snafu_tracing::{DebugTrace, trace_error, quick_tracing};

pub type Result<T, E = Error> = std::result::Result<T, E>;

#[trace_error]
#[derive(Snafu, DebugTrace)]
#[snafu(module, context(suffix(false)), visibility(pub))]
pub enum Error {
    #[snafu(display("{_error}"))]
    Any { _error: String },
    #[snafu(display("{error_source}"))]
    Wrap {
        #[snafu(source(from(Box<dyn std::error::Error + Send + Sync>, |e| e)))]
        error_source: Box<dyn std::error::Error + Send + Sync>,
    },
}

quick_tracing!(anyerr, crate::error::Any);

pub fn hello_err() -> Result<()> {
    let _e = anyerr!("Any error test! {}", 123);
    Err(anyerr!("Any error test!"))
}

fn main() {
    let e = hello_err();
    println!("{:?}", e);
}

Or refer to: full example

References

  1. Rust 错误处理在 GreptimeDB 的实践 (Rust error handling practice in GreptimeDB). Strongly recommended. Google translation should be enough for non-Chinese speakers.

Dependencies

~330–780KB
~17K SLoC