4 releases

0.1.3 Oct 25, 2024
0.1.2 Jan 11, 2021
0.1.1 Jan 11, 2021
0.1.0 Jan 10, 2021

#86 in Debugging

Download history 4838/week @ 2024-07-30 4810/week @ 2024-08-06 6328/week @ 2024-08-13 3908/week @ 2024-08-20 4168/week @ 2024-08-27 4447/week @ 2024-09-03 3876/week @ 2024-09-10 4674/week @ 2024-09-17 5049/week @ 2024-09-24 4247/week @ 2024-10-01 3196/week @ 2024-10-08 4919/week @ 2024-10-15 4983/week @ 2024-10-22 5472/week @ 2024-10-29 5070/week @ 2024-11-05 7274/week @ 2024-11-12

23,629 downloads per month
Used in 40 crates (6 directly)

MIT license

5KB

serde-error

serde-error provides a (de)serializable Error type implementing std::error::Error, that can easily be used to transmit errors over the wire.

Should I use this?

This crate is production-grade. However, you probably do not want to use it: usually, it makes much more sense to just sum up the error as some type, instead of serializing the whole causality chain.

The use case for which this crate was designed is running Rust WebAssembly blobs inside a Rust wasmtime-running host. In such a case the causality chain is clearly kept across the serialization boundary, and it thus makes sense to keep it all.

In some other cases it may make sense to serialize the whole causality chain, but most often it makes most sense to just not serialize errors.

As such, please use serde-error with parsimony.

How should I use this?

use anyhow::Context;
use std::error::Error;

fn foo() -> anyhow::Result<()> {
    // ...
    Err(anyhow::anyhow!("Failed smurfing the smurfs"))
}

fn bar() -> anyhow::Result<()> {
    // ...
    foo().context("Running foo")
}

fn main() {
    if let Err(returned_err) = bar() {
        let s = bincode::serialize(&serde_error::Error::new(&*returned_err))
            .expect("Serializing error");
        let d: serde_error::Error = bincode::deserialize(&s)
            .expect("Deserializing error");
        let e = anyhow::Error::from(d);
        assert_eq!(e.to_string(), "Running foo");
        assert_eq!(e.source().unwrap().to_string(), "Failed smurfing the smurfs");
    } else {
        panic!("bar did not return an error");
    }
}

Dependencies

~0.3–0.9MB
~21K SLoC