1use axum::{
4 http::StatusCode,
5 response::{IntoResponse, Response},
6 Json,
7};
8
9#[derive(Debug, thiserror::Error)]
11pub enum BuildError {
12 #[error("No endpoints configured. Call .with_agui() before .build()")]
14 NoEndpoints,
15}
16
17#[derive(Debug, thiserror::Error)]
19pub enum ServerError {
20 #[error("Agent error: {0}")]
22 Agent(#[from] mixtape_core::AgentError),
23
24 #[error("Permission error: {0}")]
26 Permission(String),
27
28 #[error("Invalid request: {0}")]
30 InvalidRequest(String),
31
32 #[error("Internal error: {0}")]
34 Internal(String),
35}
36
37impl IntoResponse for ServerError {
38 fn into_response(self) -> Response {
39 let (status, message) = match &self {
40 ServerError::Agent(e) => (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()),
41 ServerError::Permission(e) => (StatusCode::FORBIDDEN, e.clone()),
42 ServerError::InvalidRequest(e) => (StatusCode::BAD_REQUEST, e.clone()),
43 ServerError::Internal(e) => (StatusCode::INTERNAL_SERVER_ERROR, e.clone()),
44 };
45
46 let body = Json(serde_json::json!({
47 "error": message,
48 "code": status.as_u16(),
49 }));
50
51 (status, body).into_response()
52 }
53}
54
55pub type ServerResult<T> = Result<T, ServerError>;
57
58#[cfg(test)]
59#[path = "error_tests.rs"]
60mod tests;