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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
//! Domain error models, failure representations, and error conversion mechanics.
//!
//! Encapsulates transport-level network faults, JSON parsing discrepancies, filesystem I/O issues,
//! and remote HTTP error status codes into a unified, strongly typed [`MoonError`] enum.
//!
//! ### Public API Stability Guarantee
//!
//! Volatile third-party error types (such as [`reqwest::Error`](https://docs.rs/reqwest/latest/reqwest/struct.Error.html)
//! or [`reqwest_middleware::Error`](https://docs.rs/reqwest-middleware/latest/reqwest_middleware/struct.Error.html))
//! are boxed or coerced into string representations at the boundary. This guarantees that major version
//! bumps in underlying network crates do not induce breaking changes in downstream consumer code.
//!
//! ### Dynamic Localization (i18n)
//!
//! All error variants integrate with Project Fluent via the [`tr!`](crate::tr) macro, formatting
//! localized diagnostic descriptions dynamically according to the active execution context
//! (Tokio Task-Local or process-wide locale).
//!
//! ### Quick Example
//!
//! ```rust
//! use moonclient::{MoonError, Result};
//!
//! fn handle_failure(result: Result<()>) {
//! match result {
//! Ok(()) => println!("Transaction completed successfully!"),
//! Err(MoonError::ApiError { code, message }) => {
//! eprintln!("Remote server error HTTP {}: {}", code, message);
//! }
//! Err(MoonError::ResponseDecode { error_message, raw }) => {
//! eprintln!("Schema mismatch: {}\nRaw payload: {}", error_message, raw);
//! }
//! Err(err) => eprintln!("Network transaction failed: {}", err),
//! }
//! }
//! ```
use Deserialize;
use HashMap;
use Error;
/// Primary error type for the `moonclient` core framework.
///
/// Encapsulates transport-level network faults, parsing discrepancies, I/O failures,
/// and API validation errors. External dependency types are converted into stable internal
/// representations, guaranteeing public API stability.
// =============================================================================
// 🔄 AUTOMATIC ERROR CONVERSIONS
// =============================================================================
// =============================================================================
// 📝 REMOTE API VALIDATION MODELS (HTTP 422)
// =============================================================================
/// Representation variants for remote API validation failures (HTTP 422 Unprocessable Entity).
///
/// Uses `#[serde(untagged)]` to automatically handle both structured per-field error dictionaries
/// and flat arrays of server validation warnings.
///
/// # Example
///
/// ```rust
/// use moonclient::error::ApiErrors;
///
/// // 1. Flat message list
/// let flat: ApiErrors = serde_json::from_str(r#"["Invalid email format", "Password too short"]"#).unwrap();
/// println!("Flat errors: {}", flat);
///
/// // 2. Per-field validation map
/// let fields: ApiErrors = serde_json::from_str(r#"{"email": ["Already taken"], "age": ["Must be 18+"]}"#).unwrap();
/// println!("Field errors: {}", fields);
/// ```
/// Helper structure for deserializing remote API validation error envelopes (HTTP 422).
///
/// Many modern REST backends wrap 422 validation failures inside an `{"errors": ...}` JSON object.
/// Ergonomic type alias for [`std::result::Result`] specialized over [`MoonError`].
///
/// Commonly used across all client methods and pipelines for concise signatures.
///
/// # Example
///
/// ```rust
/// use moonclient::{MoonError, Result};
///
/// fn check_quota(limit: u32) -> Result<()> {
/// if limit > 100 {
/// return Err(MoonError::ValidationError("Quota exceeded limit of 100".to_string()));
/// }
/// Ok(())
/// }
/// ```
pub type Result<T> = Result;