rust_rfc7807/lib.rs
1//! # rust-rfc7807
2//!
3//! [RFC 7807](https://www.rfc-editor.org/rfc/rfc7807) Problem Details for HTTP APIs.
4//!
5//! This crate provides a lightweight, framework-agnostic [`Problem`] type that serializes
6//! to `application/problem+json` with safe defaults, an ergonomic builder API, and
7//! first-class support for validation errors, error codes, and trace correlation.
8//!
9//! # Quick Start
10//!
11//! ```
12//! use rust_rfc7807::Problem;
13//!
14//! let problem = Problem::not_found()
15//! .title("User not found")
16//! .detail("No user with ID 42 exists")
17//! .code("USER_NOT_FOUND");
18//!
19//! let json = serde_json::to_value(&problem).unwrap();
20//! assert_eq!(json["status"], 404);
21//! assert_eq!(json["code"], "USER_NOT_FOUND");
22//! ```
23//!
24//! # Security
25//!
26//! For 5xx errors, [`Problem`] defaults to a generic public message to prevent
27//! leaking internal information. Use [`Problem::with_cause`] to attach a diagnostic
28//! error that is **never serialized**.
29//!
30//! ```
31//! use rust_rfc7807::Problem;
32//!
33//! let problem = Problem::internal_server_error()
34//! .with_cause(std::io::Error::new(std::io::ErrorKind::Other, "db timeout"));
35//!
36//! let json = serde_json::to_string(&problem).unwrap();
37//! assert!(!json.contains("db timeout")); // never leaked
38//! ```
39//!
40//! # Validation Errors
41//!
42//! ```
43//! use rust_rfc7807::Problem;
44//!
45//! let problem = Problem::validation()
46//! .push_error("email", "must be a valid email address")
47//! .push_error_code("name", "is required", "REQUIRED");
48//!
49//! let json = serde_json::to_value(&problem).unwrap();
50//! assert_eq!(json["status"], 422);
51//! assert!(json["errors"].is_array());
52//! ```
53
54mod problem;
55mod traits;
56mod validation;
57
58#[cfg(feature = "axum")]
59mod axum_impl;
60
61pub use problem::Problem;
62pub use traits::IntoProblem;
63pub use validation::ValidationItem;
64
65/// The `Content-Type` header value for RFC 7807 problem responses.
66pub const APPLICATION_PROBLEM_JSON: &str = "application/problem+json";
67
68#[cfg(test)]
69mod tests;