#json-error #serde-json #error #serialization #serde #deserialize #deserialize-json

cdumay_error

A library to serialize and deserialize error using serde

3 unstable releases

0.3.0 Jul 5, 2024
0.2.1 Jun 21, 2024
0.2.0 Jun 21, 2024

#689 in Encoding

32 downloads per month
Used in 3 crates

BSD-3-Clause

11KB
173 lines

cdumay_error License: BSD-3-Clause cdumay_error on crates.io cdumay_error on docs.rs Source Code Repository

cdumay_error is a basic library used to standardize errors and serialize them using serde.

Quickstart

Cargo.toml:

[dependencies]
cdumay_error = "0.3"
serde_json = "1.0"

main.rs:

extern crate cdumay_error;
extern crate serde_json;

use cdumay_error::{ErrorBuilder, GenericErrors, JsonError};
use std::collections::BTreeMap;
use serde_json::Value;

fn main() {

    let err = ErrorBuilder::from(GenericErrors::GENERIC_ERROR)
        .message("This is a useless generic error.".to_string())
        .extra({
            let mut extra = BTreeMap::new();
            extra.insert("context".into(), Value::String("Example".to_string()));
            extra
        })
        .build();
    println!("{}", serde_json::to_string_pretty(&JsonError::from(err)).unwrap());
}

Output:

{
  "code": 500,
  "extra": {
    "context": "Example"
  },
  "message": "This is a useless generic error.",
  "msgid": "Err-15452"
}

Dependencies

~0.7–1.6MB
~35K SLoC