cdumay_error

Crates.iocdumay_error
lib.rscdumay_error
version0.3.0
sourcesrc
created_at2024-06-21 08:25:19.075333
updated_at2024-07-05 05:53:28.30559
descriptionA library to serialize and deserialize error using serde
homepagehttps://github.com/cdumay/rust-cdumay_error
repositoryhttps://github.com/cdumay/rust-cdumay_error
max_upload_size
id1279191
size12,358
Cédric Dumay (cdumay)

documentation

https://docs.rs/cdumay_error

README

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"
}
Commit count: 39

cargo fmt