6 releases

Uses new Rust 2024

new 0.1.8 May 15, 2025
0.1.7 May 15, 2025
0.1.5 Apr 21, 2025

#937 in Rust patterns

Download history 337/week @ 2025-04-16 75/week @ 2025-04-23 14/week @ 2025-04-30 6/week @ 2025-05-07

432 downloads per month
Used in 2 crates

Custom license

10KB

cdumay_error_json

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

A utility crate that converts serde_json::Error into structured, typed errors using the cdumay_error framework. This ensures consistent error handling, easier debugging, and informative error reporting across your Rust applications.

Features

  • Categorizes serde_json::Error into specific error types (Syntax, IO, Data, EOF)
  • Each error type is associated with a custom code, HTTP status, and descriptive message
  • Structured output for APIs, logging systems, and observability platforms
  • Includes context metadata via BTreeMap
  • Provides a convenient convert_result! macro for error conversion

Usage

Using the JsonErrorConverter directly:

use cdumay_error::ErrorConverter;
use serde_json::Value;
use std::collections::BTreeMap;
use cdumay_error_json::JsonErrorConverter;

fn parse_json(input: &str) -> Result<Value, cdumay_error::Error> {
    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_error::ErrorConverter;

fn parse_json(input: &str) -> Result<Value, cdumay_error::Error> {
    // 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")
}

Dependencies

~0.9–1.8MB
~38K SLoC