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
//! HTTP error type: a status code plus a JSON `{error, message}` body.
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use axum::Json;
use serde_json::{json, Value};
/// An API error rendered as `{ "error": <kind>, "message": <detail> }` with
/// the matching HTTP status.
#[derive(Debug)]
pub struct ApiError {
status: StatusCode,
body: Value,
}
impl ApiError {
pub fn new(status: StatusCode, kind: &str, message: String) -> Self {
Self {
status,
body: json!({ "error": kind, "message": message }),
}
}
/// 404 — unknown thread, run, or other resource.
pub fn not_found(message: String) -> Self {
Self::new(StatusCode::NOT_FOUND, "not_found", message)
}
/// 409 — multitask `reject` hit an active run, queue full, duplicate id.
pub fn conflict(message: String) -> Self {
Self::new(StatusCode::CONFLICT, "conflict", message)
}
/// 403 — authorization refused the request (R0.9 wave 2: the Cedar
/// plane forbade an admission, a grant, or an overlay). Distinct from
/// 422: the payload is well-formed and the state is not conflicting —
/// the tenant's standing policy simply does not permit this.
pub fn forbidden(message: String) -> Self {
Self::new(StatusCode::FORBIDDEN, "forbidden", message)
}
/// 429 — a tenant quota rejected the submission (R0.6 wave 3). 429, not
/// 409 or 503: the request is well-formed and the state is not
/// conflicting — the tenant is simply over its allowance, and standard
/// retry-after-backoff client behavior is exactly right.
pub fn too_many_requests(message: String) -> Self {
Self::new(StatusCode::TOO_MANY_REQUESTS, "quota_exceeded", message)
}
/// 400 — malformed payload, unknown graph, bad strategy, non-object input.
pub fn bad_request(message: String) -> Self {
Self::new(StatusCode::BAD_REQUEST, "bad_request", message)
}
/// 422 — the request is well-formed but cannot be processed: replaying a
/// run whose graph is not registered in this process, or whose journal
/// carries evidence server-side replay cannot re-drive.
pub fn unprocessable(message: String) -> Self {
Self::new(StatusCode::UNPROCESSABLE_ENTITY, "unprocessable", message)
}
/// 500 — checkpointer IO failures and other internal errors.
pub fn internal(message: String) -> Self {
Self::new(StatusCode::INTERNAL_SERVER_ERROR, "internal_error", message)
}
/// 503 — the server is draining (graceful shutdown) and rejects new
/// work. Distinct from 500 so a load balancer's retry lands on a pod
/// that is still serving.
pub fn shutting_down(message: String) -> Self {
Self::new(StatusCode::SERVICE_UNAVAILABLE, "shutting_down", message)
}
}
impl IntoResponse for ApiError {
fn into_response(self) -> Response {
(self.status, Json(self.body)).into_response()
}
}
impl std::fmt::Display for ApiError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}: {}", self.status, self.body)
}
}
impl std::error::Error for ApiError {}