use axum::{
http::StatusCode,
response::{IntoResponse, Response},
Json,
};
#[derive(Debug, thiserror::Error)]
pub enum BuildError {
#[error("No endpoints configured. Call .with_agui() before .build()")]
NoEndpoints,
}
#[derive(Debug, thiserror::Error)]
pub enum ServerError {
#[error("Agent error: {0}")]
Agent(#[from] mixtape_core::AgentError),
#[error("Permission error: {0}")]
Permission(String),
#[error("Invalid request: {0}")]
InvalidRequest(String),
#[error("Internal error: {0}")]
Internal(String),
}
impl IntoResponse for ServerError {
fn into_response(self) -> Response {
let (status, message) = match &self {
ServerError::Agent(e) => (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()),
ServerError::Permission(e) => (StatusCode::FORBIDDEN, e.clone()),
ServerError::InvalidRequest(e) => (StatusCode::BAD_REQUEST, e.clone()),
ServerError::Internal(e) => (StatusCode::INTERNAL_SERVER_ERROR, e.clone()),
};
let body = Json(serde_json::json!({
"error": message,
"code": status.as_u16(),
}));
(status, body).into_response()
}
}
pub type ServerResult<T> = Result<T, ServerError>;
#[cfg(test)]
#[path = "error_tests.rs"]
mod tests;