Crates.io | cdumay_error_json |
lib.rs | cdumay_error_json |
version | 0.1.11 |
created_at | 2025-04-20 21:03:20.040718+00 |
updated_at | 2025-05-26 13:07:00.223478+00 |
description | A Rust Library for JSON error |
homepage | https://github.com/cdumay/cdumay_error_json |
repository | https://github.com/cdumay/cdumay_error_json |
max_upload_size | |
id | 1642011 |
size | 19,830 |
A utility crate that converts serde_json::Error
into structured, typed errors using the cdumay_core
framework. This ensures consistent error handling, easier debugging, and informative error reporting across your Rust applications.
serde_json::Error
into specific error types (Syntax
, IO
, Data
, EOF
)BTreeMap
convert_result!
macro for error conversionUsing the JsonErrorConverter
directly:
use cdumay_core::{Error, ErrorConverter};
use serde_json::Value;
use std::collections::BTreeMap;
use cdumay_error_json::JsonErrorConverter;
fn parse_json(input: &str) -> cdumay_core::Result<Value> {
serde_json::from_str::<Value>(input).map_err(|e| {
let mut ctx = BTreeMap::new();
ctx.insert("input".to_string(), serde_value::Value::String(input.to_string()));
JsonErrorConverter::convert(&e, "Failed to parse JSON".to_string(), ctx)
})
}
Using the convert_result!
macro:
use cdumay_error_json::convert_result;
use serde_json::Value;
use std::collections::BTreeMap;
use cdumay_core::{Error, ErrorConverter};
fn parse_json(input: &str) -> cdumay_core::Result<Value> {
// Basic usage with just the result
convert_result!(serde_json::from_str::<Value>(input));
// With custom context
let mut ctx = BTreeMap::new();
ctx.insert("input".to_string(), serde_value::Value::String(input.to_string()));
convert_result!(serde_json::from_str::<Value>(input), ctx.clone());
// With custom context and message
convert_result!(serde_json::from_str::<Value>(input), ctx, "Failed to parse JSON")
}