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
use axum::{
Json,
http::StatusCode,
response::{IntoResponse, Response},
};
use serde::Serialize;
use crate::{AuthError, FileError, RuntimeError, WebhookError};
/// Standardised JSON error body returned by all FraiseQL HTTP endpoints.
///
/// The shape follows the OAuth 2.0 error response convention so that clients
/// can handle errors uniformly regardless of which handler produced them.
///
/// Fields that are `None` are omitted from the serialised JSON to keep
/// responses compact.
#[derive(Debug, Serialize)]
pub struct ErrorResponse {
/// Short machine-readable error category (e.g. `"authentication_error"`).
pub error: String,
/// Human-readable description safe to display to end-users.
pub error_description: String,
/// Stable, fine-grained error code for programmatic handling (e.g.
/// `"token_expired"`).
pub error_code: String,
/// URL to the documentation page for this error code, if available.
#[serde(skip_serializing_if = "Option::is_none")]
pub error_uri: Option<String>,
/// Additional structured details about the error (omitted when `None`).
#[serde(skip_serializing_if = "Option::is_none")]
pub details: Option<serde_json::Value>,
/// Number of seconds the client should wait before retrying, if applicable.
#[serde(skip_serializing_if = "Option::is_none")]
pub retry_after: Option<u64>,
}
impl ErrorResponse {
/// Construct a minimal error response with the three required fields.
///
/// `error_uri` is populated automatically from `code` using the FraiseQL
/// documentation base URL. `details` and `retry_after` are `None`.
pub fn new(
error: impl Into<String>,
description: impl Into<String>,
code: impl Into<String>,
) -> Self {
let code = code.into();
Self {
error: error.into(),
error_description: description.into(),
error_uri: Some(format!("https://docs.fraiseql.dev/errors#{}", code)),
error_code: code,
details: None,
retry_after: None,
}
}
/// Attach arbitrary structured details to the response and return `self`.
pub fn with_details(mut self, details: serde_json::Value) -> Self {
self.details = Some(details);
self
}
/// Set the `retry_after` field (in seconds) and return `self`.
pub const fn with_retry_after(mut self, seconds: u64) -> Self {
self.retry_after = Some(seconds);
self
}
}
impl IntoResponse for RuntimeError {
fn into_response(self) -> Response {
let error_code = self.error_code();
let (status, response) = match &self {
RuntimeError::Config(_) => (
StatusCode::INTERNAL_SERVER_ERROR,
// SECURITY: Config errors may contain connection strings or secrets.
// Return a generic message; details are in server logs only.
ErrorResponse::new(
"configuration_error",
"A configuration error occurred",
error_code,
),
),
RuntimeError::Auth(e) => {
let (status, msg) = match e {
AuthError::InsufficientPermissions { .. } => {
(StatusCode::FORBIDDEN, "Insufficient permissions")
},
AuthError::AccountLocked { .. } => (StatusCode::FORBIDDEN, "Account locked"),
AuthError::InvalidCredentials => {
(StatusCode::UNAUTHORIZED, "Invalid credentials")
},
AuthError::TokenExpired => (StatusCode::UNAUTHORIZED, "Token expired"),
// SECURITY: InvalidToken, ProviderError messages may contain internal details
// (JWT parsing reasons, provider endpoint URLs). Return generic message.
AuthError::InvalidToken { .. } | AuthError::ProviderError { .. } => {
(StatusCode::UNAUTHORIZED, "Authentication failed")
},
AuthError::InvalidState => (StatusCode::UNAUTHORIZED, "Invalid OAuth state"),
AuthError::UserDenied => {
(StatusCode::UNAUTHORIZED, "User denied authorization")
},
AuthError::SessionNotFound | AuthError::SessionExpired => {
(StatusCode::UNAUTHORIZED, "Session not found or expired")
},
AuthError::RefreshTokenInvalid => {
(StatusCode::UNAUTHORIZED, "Refresh token invalid or expired")
},
};
(status, ErrorResponse::new("authentication_error", msg, error_code))
},
RuntimeError::Webhook(e) => {
let (status, msg) = match e {
WebhookError::InvalidSignature => {
(StatusCode::UNAUTHORIZED, "Invalid webhook signature")
},
WebhookError::DuplicateEvent { .. } => (StatusCode::OK, "Duplicate event"),
WebhookError::TimestampExpired { .. } => {
(StatusCode::BAD_REQUEST, "Webhook timestamp expired — check your clock")
},
WebhookError::TimestampFuture { .. } => {
(StatusCode::BAD_REQUEST, "Webhook timestamp is in the future")
},
WebhookError::MissingSignature { .. } => {
(StatusCode::BAD_REQUEST, "Missing webhook signature header")
},
WebhookError::UnknownEvent { .. } => {
(StatusCode::BAD_REQUEST, "Unknown webhook event type")
},
WebhookError::ProviderNotConfigured { .. } => {
(StatusCode::BAD_REQUEST, "Webhook provider not configured")
},
// SECURITY: PayloadError and IdempotencyError messages may contain
// internal parsing details. Return generic messages.
WebhookError::PayloadError { .. } | WebhookError::IdempotencyError { .. } => {
(StatusCode::BAD_REQUEST, "Webhook processing failed")
},
};
(status, ErrorResponse::new("webhook_error", msg, error_code))
},
RuntimeError::File(e) => {
let (status, msg) = match e {
FileError::TooLarge { size, max } => (
StatusCode::PAYLOAD_TOO_LARGE,
// Safe to expose size info — helps client fix the request.
format!("File too large: {} bytes exceeds maximum {}", size, max),
),
FileError::InvalidType { .. } | FileError::MimeMismatch { .. } => {
(StatusCode::UNSUPPORTED_MEDIA_TYPE, "Unsupported file type".to_string())
},
FileError::NotFound { .. } => {
// SECURITY: Do not expose internal file paths.
(StatusCode::NOT_FOUND, "File not found".to_string())
},
FileError::VirusDetected { .. } => {
(StatusCode::UNPROCESSABLE_ENTITY, "File failed security scan".to_string())
},
FileError::QuotaExceeded => {
(StatusCode::INSUFFICIENT_STORAGE, "Storage quota exceeded".to_string())
},
_ => (StatusCode::BAD_REQUEST, "File operation failed".to_string()),
};
(status, ErrorResponse::new("file_error", msg, error_code))
},
RuntimeError::Notification(e) => {
use crate::NotificationError::{
CircuitOpen, InvalidInput, ProviderRateLimited, ProviderUnavailable,
};
let status = match e {
CircuitOpen { .. } | ProviderUnavailable { .. } => {
StatusCode::SERVICE_UNAVAILABLE
},
ProviderRateLimited { .. } => StatusCode::TOO_MANY_REQUESTS,
InvalidInput { .. } => StatusCode::BAD_REQUEST,
_ => StatusCode::INTERNAL_SERVER_ERROR,
};
// SECURITY: Provider details (API keys, endpoints) must not appear in responses.
let msg = match e {
InvalidInput { .. } => self.to_string(),
_ => "Notification service unavailable".to_string(),
};
(status, ErrorResponse::new("notification_error", msg, error_code))
},
RuntimeError::RateLimited { retry_after } => {
let mut resp =
ErrorResponse::new("rate_limited", "Rate limit exceeded", error_code);
if let Some(secs) = retry_after {
resp = resp.with_retry_after(*secs);
}
(StatusCode::TOO_MANY_REQUESTS, resp)
},
RuntimeError::ServiceUnavailable { retry_after, .. } => {
// SECURITY: ServiceUnavailable may contain internal service names or endpoints.
// Return a generic message; details are in server logs only.
let mut resp = ErrorResponse::new(
"service_unavailable",
"Service temporarily unavailable",
error_code,
);
if let Some(secs) = retry_after {
resp = resp.with_retry_after(*secs);
}
(StatusCode::SERVICE_UNAVAILABLE, resp)
},
RuntimeError::NotFound { .. } => (
StatusCode::NOT_FOUND,
// SECURITY: Do not expose internal resource names or IDs.
ErrorResponse::new("not_found", "Resource not found", error_code),
),
RuntimeError::Database(_) => (
StatusCode::INTERNAL_SERVER_ERROR,
ErrorResponse::new("database_error", "A database error occurred", error_code),
),
_ => (
StatusCode::INTERNAL_SERVER_ERROR,
ErrorResponse::new("internal_error", "An internal error occurred", error_code),
),
};
// Add Retry-After header for rate limits
let mut resp = (status, Json(response)).into_response();
if let Some(retry_after) = self.retry_after_header() {
if let Ok(value) = retry_after.parse() {
resp.headers_mut().insert("Retry-After", value);
}
}
resp
}
}
impl RuntimeError {
fn retry_after_header(&self) -> Option<String> {
match self {
Self::RateLimited {
retry_after: Some(secs),
}
| Self::ServiceUnavailable {
retry_after: Some(secs),
..
} => Some(secs.to_string()),
_ => None,
}
}
}
/// Convenience trait that allows returning `Result<T, RuntimeError>` from axum
/// handlers by converting it directly into an HTTP [`Response`].
///
/// Import this trait and call `.into_http_response()` on any
/// `Result<impl IntoResponse, RuntimeError>` value.
pub trait IntoHttpResponse {
/// Convert this result into an axum [`Response`].
///
/// On success the inner value is serialised via its own [`IntoResponse`]
/// implementation. On error the [`RuntimeError`] is converted to a JSON
/// error body with the appropriate HTTP status code.
fn into_http_response(self) -> Response;
}
impl<T> IntoHttpResponse for Result<T, RuntimeError>
where
T: IntoResponse,
{
fn into_http_response(self) -> Response {
match self {
Ok(value) => value.into_response(),
Err(err) => err.into_response(),
}
}
}