Crates.io | cdumay_error |
lib.rs | cdumay_error |
version | 0.3.0 |
source | src |
created_at | 2024-06-21 08:25:19.075333 |
updated_at | 2024-07-05 05:53:28.30559 |
description | A library to serialize and deserialize error using serde |
homepage | https://github.com/cdumay/rust-cdumay_error |
repository | https://github.com/cdumay/rust-cdumay_error |
max_upload_size | |
id | 1279191 |
size | 12,358 |
cdumay_error is a basic library used to standardize errors and serialize them using serde.
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"
}