Skip to main content

rusty_agent_server/
error.rs

1//! HTTP error type: a status code plus a JSON `{error, message}` body.
2
3use axum::http::StatusCode;
4use axum::response::{IntoResponse, Response};
5use axum::Json;
6use serde_json::{json, Value};
7
8/// An API error rendered as `{ "error": <kind>, "message": <detail> }` with
9/// the matching HTTP status.
10#[derive(Debug)]
11pub struct ApiError {
12    status: StatusCode,
13    body: Value,
14}
15
16impl ApiError {
17    pub fn new(status: StatusCode, kind: &str, message: String) -> Self {
18        Self {
19            status,
20            body: json!({ "error": kind, "message": message }),
21        }
22    }
23
24    /// 404 — unknown thread, run, or other resource.
25    pub fn not_found(message: String) -> Self {
26        Self::new(StatusCode::NOT_FOUND, "not_found", message)
27    }
28
29    /// 409 — multitask `reject` hit an active run, queue full, duplicate id.
30    pub fn conflict(message: String) -> Self {
31        Self::new(StatusCode::CONFLICT, "conflict", message)
32    }
33
34    /// 403 — authorization refused the request (R0.9 wave 2: the Cedar
35    /// plane forbade an admission, a grant, or an overlay). Distinct from
36    /// 422: the payload is well-formed and the state is not conflicting —
37    /// the tenant's standing policy simply does not permit this.
38    pub fn forbidden(message: String) -> Self {
39        Self::new(StatusCode::FORBIDDEN, "forbidden", message)
40    }
41
42    /// 429 — a tenant quota rejected the submission (R0.6 wave 3). 429, not
43    /// 409 or 503: the request is well-formed and the state is not
44    /// conflicting — the tenant is simply over its allowance, and standard
45    /// retry-after-backoff client behavior is exactly right.
46    pub fn too_many_requests(message: String) -> Self {
47        Self::new(StatusCode::TOO_MANY_REQUESTS, "quota_exceeded", message)
48    }
49
50    /// 400 — malformed payload, unknown graph, bad strategy, non-object input.
51    pub fn bad_request(message: String) -> Self {
52        Self::new(StatusCode::BAD_REQUEST, "bad_request", message)
53    }
54
55    /// 422 — the request is well-formed but cannot be processed: replaying a
56    /// run whose graph is not registered in this process, or whose journal
57    /// carries evidence server-side replay cannot re-drive.
58    pub fn unprocessable(message: String) -> Self {
59        Self::new(StatusCode::UNPROCESSABLE_ENTITY, "unprocessable", message)
60    }
61
62    /// 500 — checkpointer IO failures and other internal errors.
63    pub fn internal(message: String) -> Self {
64        Self::new(StatusCode::INTERNAL_SERVER_ERROR, "internal_error", message)
65    }
66
67    /// 503 — the server is draining (graceful shutdown) and rejects new
68    /// work. Distinct from 500 so a load balancer's retry lands on a pod
69    /// that is still serving.
70    pub fn shutting_down(message: String) -> Self {
71        Self::new(StatusCode::SERVICE_UNAVAILABLE, "shutting_down", message)
72    }
73}
74
75impl IntoResponse for ApiError {
76    fn into_response(self) -> Response {
77        (self.status, Json(self.body)).into_response()
78    }
79}
80
81impl std::fmt::Display for ApiError {
82    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
83        write!(f, "{}: {}", self.status, self.body)
84    }
85}
86
87impl std::error::Error for ApiError {}