Skip to main content

mixtape_server/
error.rs

1//! Error types for the mixtape server.
2
3use axum::{
4    http::StatusCode,
5    response::{IntoResponse, Response},
6    Json,
7};
8
9/// Errors that can occur when building a router.
10#[derive(Debug, thiserror::Error)]
11pub enum BuildError {
12    /// No endpoints were configured.
13    #[error("No endpoints configured. Call .with_agui() before .build()")]
14    NoEndpoints,
15}
16
17/// Errors that can occur in the mixtape server.
18#[derive(Debug, thiserror::Error)]
19pub enum ServerError {
20    /// Error from the agent during execution.
21    #[error("Agent error: {0}")]
22    Agent(#[from] mixtape_core::AgentError),
23
24    /// Permission-related error.
25    #[error("Permission error: {0}")]
26    Permission(String),
27
28    /// Invalid request from client.
29    #[error("Invalid request: {0}")]
30    InvalidRequest(String),
31
32    /// Internal server error.
33    #[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
55/// Result type alias for server operations.
56pub type ServerResult<T> = Result<T, ServerError>;
57
58#[cfg(test)]
59#[path = "error_tests.rs"]
60mod tests;