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
//! Structured error handling with multi-format output.
//!
//! Define errors once and render them to JSON, GraphQL, HTML, or plain text.
//!
//! # Quick Start
//!
//! ```rust
//! use apollo_errors::Error;
//! use miette::Diagnostic;
//!
//! #[derive(Debug, Error, Diagnostic)]
//! pub enum AuthError {
//! #[error("Invalid credentials for user {username}")]
//! #[diagnostic(code(auth::invalid_credentials))]
//! InvalidCredentials {
//! #[extension]
//! username: String,
//! },
//! }
//!
//! let error = AuthError::InvalidCredentials {
//! username: "alice".to_string(),
//! };
//!
//! // Render to different formats
//! let json = error.to_json().unwrap();
//! let graphql = error.to_graphql().unwrap();
//! let jsonrpc = error.to_jsonrpc().unwrap();
//! let html = error.to_html();
//! let text = error.to_text();
//! ```
//!
//! # Defining Errors
//!
//! Errors require three derives: `Debug`, `Error`, and `Diagnostic`.
//!
//! ```rust,ignore
//! #[derive(Debug, Error, Diagnostic)]
//! pub enum MyError {
//! #[error("Something went wrong")]
//! #[diagnostic(code(service::something_wrong))]
//! SomethingWrong,
//! }
//! ```
//!
//! # Attributes
//!
//! ## Error Message
//!
//! Use `#[error("...")]` to define the error message. Field interpolation is supported:
//!
//! ```rust,ignore
//! #[error("Failed to connect to {host}:{port}")]
//! ConnectionFailed { host: String, port: u16 },
//! ```
//!
//! ## Error Code
//!
//! Use `#[diagnostic(code(...))]` to define a unique error code. Codes must have
//! at least two `::` separated segments, all lowercase:
//!
//! ```rust,ignore
//! #[diagnostic(code(db::connection_failed))]
//! #[diagnostic(code(auth::invalid_token))]
//! ```
//!
//! ## Extension Fields
//!
//! Mark fields with `#[extension]` to include them in JSON and GraphQL output:
//!
//! ```rust,ignore
//! InvalidPort {
//! #[extension]
//! port: u16,
//! #[extension]
//! config_file: String,
//! },
//! ```
//!
//! ## HTTP Status
//!
//! Specify an HTTP status code (defaults to 500):
//!
//! ```rust,ignore
//! #[http_status(404)]
//! NotFound,
//! ```
//!
//! ## HTTP Headers
//!
//! Mark fields to be returned as HTTP response headers. Supported types are `u16`, `u32`,
//! `u64`, `i16`, `i32`, `i64`, `bool`, and `HeaderValue`:
//!
//! ```rust,ignore
//! #[http_status(429)]
//! RateLimitExceeded {
//! #[http_header("Retry-After")]
//! retry_after: u64,
//! #[http_header("X-RateLimit-Remaining")]
//! remaining: u32,
//! },
//! ```
//!
//! Header names are validated at compile time against RFC 7230. Headers are
//! automatically set when using [`tower_http::ErrorLayer`]. For `Option<T>` fields,
//! the header is only included when the value is `Some`.
//!
//! ## JSON-RPC Code
//!
//! Specify a JSON-RPC 2.0 error code (defaults to -32000, "Server error"):
//!
//! ```rust,ignore
//! #[jsonrpc_code(-32602)]
//! InvalidParams { param: String },
//! ```
//!
//! Reserved JSON-RPC codes:
//! - `-32700`: Parse error
//! - `-32600`: Invalid Request
//! - `-32601`: Method not found
//! - `-32602`: Invalid params
//! - `-32603`: Internal error
//! - `-32000` to `-32099`: Server error (available for application use)
//!
//! ## Help Text and URLs
//!
//! Provide additional context for users:
//!
//! ```rust,ignore
//! #[diagnostic(
//! code(config::invalid),
//! help("Check your configuration file"),
//! url("https://docs.example.com/errors/config-invalid")
//! )]
//! ```
//!
//! ## Error Chaining
//!
//! Use `#[source]` to chain errors, or `#[from]` to also generate a `From` impl:
//!
//! ```rust,ignore
//! #[error("Database operation failed")]
//! #[diagnostic(code(db::operation_failed))]
//! DatabaseError {
//! #[from]
//! source: std::io::Error,
//! },
//! ```
//!
//! # Dynamic Dispatch
//!
//! Format any `std::error::Error` using the extension traits:
//!
//! ```rust
//! use apollo_errors::{ErrorExt, HeapErrorExt};
//!
//! fn handle_error(error: Box<dyn std::error::Error + Send + Sync>) {
//! let json = error.to_json().unwrap();
//! println!("{}", json);
//! }
//! ```
// Re-export the derive macro
pub use Error;
// Re-export miette for user convenience
pub use miette;
// Re-export http crate for Error trait return types
pub use http;
pub use CatalogErrorEntry as ErrorMetadata;
pub use CatalogVariantEntry as ErrorVariantMetadata;
pub use error_catalog;
pub use Error;
pub use ;
pub use FieldMetadata as ErrorFieldMetadata;