rusty_agent_server/
error.rs1use axum::http::StatusCode;
4use axum::response::{IntoResponse, Response};
5use axum::Json;
6use serde_json::{json, Value};
7
8#[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 pub fn not_found(message: String) -> Self {
26 Self::new(StatusCode::NOT_FOUND, "not_found", message)
27 }
28
29 pub fn conflict(message: String) -> Self {
31 Self::new(StatusCode::CONFLICT, "conflict", message)
32 }
33
34 pub fn forbidden(message: String) -> Self {
39 Self::new(StatusCode::FORBIDDEN, "forbidden", message)
40 }
41
42 pub fn too_many_requests(message: String) -> Self {
47 Self::new(StatusCode::TOO_MANY_REQUESTS, "quota_exceeded", message)
48 }
49
50 pub fn bad_request(message: String) -> Self {
52 Self::new(StatusCode::BAD_REQUEST, "bad_request", message)
53 }
54
55 pub fn unprocessable(message: String) -> Self {
59 Self::new(StatusCode::UNPROCESSABLE_ENTITY, "unprocessable", message)
60 }
61
62 pub fn internal(message: String) -> Self {
64 Self::new(StatusCode::INTERNAL_SERVER_ERROR, "internal_error", message)
65 }
66
67 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 {}