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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// SPDX-FileCopyrightText: 2025 RAprogramm <andrey.rozanov.vl@gmail.com>
//
// SPDX-License-Identifier: MIT
//! Framework-agnostic application error types for backend services.
//!
//! # Overview
//!
//! A small, pragmatic error model designed for API-heavy services.
//! The core is framework-agnostic; integrations are optional and enabled via
//! feature flags.
//!
//! Core types:
//! - [`AppError`] — rich error capturing code, taxonomy, message, metadata and
//! transport hints
//! - [`AppErrorKind`] — stable internal taxonomy of application errors
//! - [`AppResult`] — convenience alias for returning [`AppError`]
//! - [`ProblemJson`] — RFC7807 payload emitted by HTTP/gRPC adapters
//! - [`ErrorResponse`] — legacy wire-level JSON payload for HTTP APIs
//! - [`AppCode`] — public, machine-readable error code for clients
//! - [`Metadata`] — structured telemetry attached to [`AppError`]
//! - [`field`] — helper functions to build [`Metadata`] without manual enums
//!
//! Key properties:
//! - Stable, predictable error categories (`AppErrorKind`).
//! - Explicit, overridable machine-readable codes (`AppCode`).
//! - Structured metadata for observability without ad-hoc `String` maps.
//! - Conservative and stable HTTP mappings.
//! - Internal error sources are never serialized to clients (only logged).
//! - Messages are safe to expose (human-oriented, non-sensitive).
//!
//! # Minimum Supported Rust Version (MSRV)
//!
//! MSRV is **1.90**. New minor releases may increase MSRV with a changelog
//! note, but never in a patch release.
//!
//! # Feature flags
//!
//! Enable only what you need:
//!
//! - `axum` — implements `IntoResponse` for [`AppError`] and [`ProblemJson`]
//! with RFC7807 body
//! - `actix` — implements `Responder` for [`ProblemJson`] and Actix
//! `ResponseError` for [`AppError`]
//! - `tonic` — converts [`struct@Error`] into `tonic::Status` with sanitized
//! metadata
//! - `openapi` — derives an OpenAPI schema for [`ErrorResponse`] (via `utoipa`)
//! - `sqlx` — `From<sqlx::Error>` mapping
//! - `redis` — `From<redis::RedisError>` mapping
//! - `validator` — `From<validator::ValidationErrors>` mapping
//! - `config` — `From<config::ConfigError>` mapping
//! - `tokio` — `From<tokio::time::error::Elapsed>` mapping
//! - `reqwest` — `From<reqwest::Error>` mapping
//! - `teloxide` — `From<teloxide_core::RequestError>` mapping
//! - `telegram-webapp-sdk` —
//! `From<telegram_webapp_sdk::utils::validate_init_data::ValidationError>`
//! mapping
//! - `frontend` — convert errors into `wasm_bindgen::JsValue` and emit
//! `console.error` logs in WASM/browser contexts
//! - `serde_json` — support for structured JSON details in [`ErrorResponse`]
//! and [`ProblemJson`]; also pulled transitively by `axum`
//! - `multipart` — compatibility flag for Axum multipart
//! - `turnkey` — domain taxonomy and conversions for Turnkey errors, exposed in
//! the `turnkey` module
//!
//! # Derive macros and telemetry
//!
//! The [`masterror::Error`](derive@crate::Error) derive mirrors `thiserror`
//! while adding `#[app_error]` and `#[provide]` attributes. Annotate your
//! domain errors once to surface structured telemetry via
//! [`std::error::Request`] and generate conversions into [`AppError`] /
//! [`AppCode`].
//!
//! ```rust
//! use masterror::{AppCode, AppError, AppErrorKind, Error};
//!
//! #[derive(Debug, Error)]
//! #[error("missing flag: {name}")]
//! #[app_error(kind = AppErrorKind::BadRequest, code = AppCode::BadRequest, message)]
//! struct MissingFlag {
//! name: &'static str
//! }
//!
//! let app: AppError = MissingFlag {
//! name: "feature"
//! }
//! .into();
//! assert!(matches!(app.kind, AppErrorKind::BadRequest));
//! ```
//!
//! Use `#[provide]` to forward typed telemetry that downstream consumers can
//! extract from [`AppError`] via `std::error::Request`.
//!
//! ## Masterror derive: end-to-end domain errors
//!
//! `#[derive(Masterror)]` builds on top of `#[derive(Error)]`, wiring a domain
//! error directly into [`struct@crate::Error`] with typed telemetry, redaction
//! policy and transport hints. The `#[masterror(...)]` attribute mirrors the
//! `thiserror` style while keeping redaction decisions and metadata in one
//! place.
//!
//! ```rust
//! use masterror::{
//! AppCode, AppErrorKind, Error, Masterror, MessageEditPolicy, mapping::HttpMapping
//! };
//!
//! #[derive(Debug, Masterror)]
//! #[error("user {user_id} missing flag {flag}")]
//! #[masterror(
//! code = AppCode::NotFound,
//! category = AppErrorKind::NotFound,
//! message,
//! redact(message, fields("user_id" = hash)),
//! telemetry(
//! Some(masterror::field::str("user_id", user_id.clone())),
//! attempt.map(|value| masterror::field::u64("attempt", value))
//! ),
//! map.grpc = 5,
//! map.problem = "https://errors.example.com/not-found"
//! )]
//! struct MissingFlag {
//! user_id: String,
//! flag: &'static str,
//! attempt: Option<u64>,
//! #[source]
//! source: Option<std::io::Error>
//! }
//!
//! let err = MissingFlag {
//! user_id: "alice".into(),
//! flag: "beta",
//! attempt: Some(2),
//! source: None
//! };
//! let converted: Error = err.into();
//! assert_eq!(converted.code, AppCode::NotFound);
//! assert_eq!(converted.kind, AppErrorKind::NotFound);
//! assert_eq!(converted.edit_policy, MessageEditPolicy::Redact);
//! assert!(converted.metadata().get("user_id").is_some());
//! assert_eq!(
//! MissingFlag::HTTP_MAPPING,
//! HttpMapping::new(AppCode::NotFound, AppErrorKind::NotFound)
//! );
//! ```
//!
//! - `code` — public [`AppCode`].
//! - `category` — semantic [`AppErrorKind`].
//! - `message` — expose the formatted [`core::fmt::Display`] output as the
//! public message.
//! - `redact(message)` — mark the message as redactable at the transport
//! boundary, `fields("name" = hash, "card" = last4)` override metadata
//! policies (`hash`, `last4`, `redact`, `none`).
//! - `telemetry(...)` — list of expressions producing
//! `Option<masterror::Field>` to be inserted into [`Metadata`].
//! - `map.grpc` / `map.problem` — optional gRPC status (as `i32`) and
//! problem+json type for generated mapping tables. Access them via
//! `TYPE::HTTP_MAPPING`, `TYPE::GRPC_MAPPING`/`MAPPINGS` and
//! `TYPE::PROBLEM_MAPPING`/`MAPPINGS`.
//!
//! The derive continues to honour `#[from]`, `#[source]` and `#[backtrace]`
//! field attributes, automatically attaching sources and captured backtraces to
//! the resulting [`struct@Error`].
//!
//! # Domain integrations: Turnkey
//!
//! With the `turnkey` feature enabled, the crate exports a `turnkey` module
//! that provides:
//!
//! - `turnkey::TurnkeyErrorKind` — stable categories for Turnkey-specific
//! failures
//! - `turnkey::TurnkeyError` — a container with `kind` and safe, public message
//! - `turnkey::classify_turnkey_error` — heuristic classifier for raw
//! SDK/provider strings
//! - conversions: `From<TurnkeyError>` → [`AppError`] and
//! `From<TurnkeyErrorKind>` → [`AppErrorKind`]
//!
//! ## Example
//!
//! ```rust
//! # #[cfg(feature = "turnkey")]
//! # {
//! use masterror::{
//! AppError, AppErrorKind,
//! turnkey::{TurnkeyError, TurnkeyErrorKind, classify_turnkey_error}
//! };
//!
//! // Classify a raw provider message
//! let kind = classify_turnkey_error("429 Too Many Requests");
//! assert!(matches!(kind, TurnkeyErrorKind::RateLimited));
//!
//! // Build and convert into AppError
//! let e = TurnkeyError::new(TurnkeyErrorKind::RateLimited, "throttled by upstream");
//! let app: AppError = e.into();
//! assert_eq!(app.kind, AppErrorKind::RateLimited);
//! # }
//! ```
//!
//! # Error taxonomy
//!
//! Applications convert domain/infrastructure failures into [`AppError`] with a
//! semantic [`AppErrorKind`] and optional public message:
//!
//! ```rust
//! use masterror::{AppError, AppErrorKind};
//!
//! let err = AppError::new(AppErrorKind::BadRequest, "Flag must be set");
//! assert!(matches!(err.kind, AppErrorKind::BadRequest));
//! ```
//!
//! Attach structured metadata for telemetry and logging:
//! ```rust
//! use masterror::{AppError, AppErrorKind, field};
//!
//! let err = AppError::service("downstream degraded")
//! .with_field(field::str("request_id", "abc123"))
//! .with_field(field::i64("attempt", 2));
//! assert_eq!(err.metadata().len(), 2);
//! ```
//!
//! Attach upstream diagnostics without cloning existing `Arc`s:
//! ```rust
//! # #[cfg(feature = "std")] {
//! use masterror::AppError;
//!
//! let err = AppError::internal("db down")
//! .with_context(std::io::Error::new(std::io::ErrorKind::Other, "boom"));
//! assert!(err.source_ref().is_some());
//! # }
//! ```
//!
//! [`AppErrorKind`] controls the default HTTP status mapping.
//! [`AppCode`] provides a stable machine-readable code for clients.
//! Together, they form the wire contract in [`ErrorResponse`].
//!
//! # Wire payload: [`ErrorResponse`]
//!
//! The stable JSON payload for HTTP APIs contains:
//! - `status: u16` — HTTP status code
//! - `code: AppCode` — stable machine-readable error code
//! - `message: String` — human-friendly, safe-to-expose text
//! - `details` — optional details (JSON if `serde_json`, otherwise string)
//! - `retry` — optional retry advice (`Retry-After`)
//! - `www_authenticate` — optional authentication challenge
//!
//! Example construction:
//!
//! ```rust
//! use masterror::{AppCode, ErrorResponse};
//!
//! let resp = ErrorResponse::new(404, AppCode::NotFound, "User not found").expect("status");
//! ```
//!
//! Conversion from [`AppError`]:
//!
//! ```rust
//! use masterror::{AppCode, AppError, AppErrorKind, ErrorResponse};
//!
//! let app_err = AppError::new(AppErrorKind::NotFound, "user_not_found");
//! let resp: ErrorResponse = (&app_err).into();
//! assert_eq!(resp.status, 404);
//! assert_eq!(resp.code, AppCode::NotFound);
//! ```
//!
//! # Typed control-flow macros
//!
//! Reach for [`ensure!`] and [`fail!`] when you need to exit early with a typed
//! error without paying for string formatting or heap allocations on the
//! success path.
//!
//! ```rust
//! use masterror::{AppError, AppErrorKind, AppResult};
//!
//! fn guard(flag: bool) -> AppResult<()> {
//! masterror::ensure!(flag, AppError::bad_request("flag must be set"));
//! Ok(())
//! }
//!
//! fn bail() -> AppResult<()> {
//! masterror::fail!(AppError::unauthorized("token expired"));
//! }
//!
//! assert!(guard(true).is_ok());
//! assert!(matches!(
//! guard(false).unwrap_err().kind,
//! AppErrorKind::BadRequest
//! ));
//! assert!(matches!(
//! bail().unwrap_err().kind,
//! AppErrorKind::Unauthorized
//! ));
//! ```
//!
//! # Axum integration
//!
//! With the `axum` feature enabled, you can return [`AppError`] directly from
//! handlers. It is automatically converted into an [`ErrorResponse`] JSON
//! payload.
//!
//! ```rust,ignore
//! use axum::{routing::get, Router};
//! use masterror::{AppError, AppResult};
//!
//! async fn handler() -> AppResult<&'static str> {
//! Err(AppError::forbidden("No access"))
//! }
//!
//! let app = Router::new().route("/demo", get(handler));
//! ```
//!
//! # OpenAPI integration
//!
//! With the `openapi` feature enabled, [`ErrorResponse`] derives
//! `utoipa::ToSchema` and can be referenced in OpenAPI operation responses.
//!
//! # Versioning policy
//!
//! This crate follows semantic versioning. Any change to the public API
//! or wire contract is considered a **breaking change** and requires a major
//! version bump.
//!
//! # Safety
//!
//! This crate does not use `unsafe`.
//!
//! # License
//!
//! Licensed under either of
//! - Apache License, Version 2.0
//! - MIT license
//!
//! at your option.
extern crate alloc;
/// Minimal prelude re-exporting core types for handler signatures.
/// Transport mapping descriptors for generated domain errors.
pub use ;
pub use ;
pub use AppErrorKind;
/// Re-export derive macros so users only depend on this crate.
///
/// # Examples
///
/// ```
/// use masterror::{AppCode, AppError, AppErrorKind, Error};
///
/// #[derive(Debug, Error)]
/// #[error("missing flag: {name}")]
/// #[app_error(kind = AppErrorKind::BadRequest, code = AppCode::BadRequest, message)]
/// struct MissingFlag {
/// name: &'static str
/// }
///
/// let app: AppError = MissingFlag {
/// name: "feature"
/// }
/// .into();
/// assert!(matches!(app.kind, AppErrorKind::BadRequest));
///
/// let code: AppCode = MissingFlag {
/// name: "other"
/// }
/// .into();
/// assert_eq!(code, AppCode::BadRequest);
/// ```
pub use ;
pub use ;
pub use ResultExt;
pub use crateStatusConversionError;