1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//! [](./LICENSE)
//! [](https://crates.io/crates/cdumay_error_json)
//! [](https://docs.rs/cdumay_error_json)
//! [](https://github.com/cdumay/cdumay_error_json)
//!
//! A utility crate that converts `serde_json::Error` into structured, typed errors using the [`cdumay_core`](https://docs.rs/cdumay_core/) 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:
//! ```rust
//! 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:
//! ```rust
//! 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")
//! }
//! ```
use ;
use Category;
use BTreeMap;
define_kinds!
define_errors!
/// A utility struct for handling JSON errors and converting them into standardized error types.
;