use axum::{ http::StatusCode, response::{IntoResponse, Response}, Json, }; use serde_json::json; use thiserror::Error; #[derive(Debug, Error)] pub enum OrchestratorError { #[error("texts must be non-empty")] EmptyTexts, #[error("text[{index}] exceeds context window ({tokens}>{limit})")] TextTooLong { index: usize, tokens: usize, limit: usize, }, #[error("only 0.6B is supported")] UnsupportedSize, #[error("tokenizer error: {0}")] Tokenizer(String), #[error("swift backend error: {0}")] Swift(String), #[error("rust backend error: {0}")] Rust(String), #[error("internal: {0}")] Internal(String), } impl IntoResponse for OrchestratorError { fn into_response(self) -> Response { let status = match &self { Self::EmptyTexts | Self::TextTooLong { .. } | Self::UnsupportedSize => { StatusCode::BAD_REQUEST } Self::Tokenizer(_) | Self::Swift(_) | Self::Rust(_) | Self::Internal(_) => { StatusCode::INTERNAL_SERVER_ERROR } }; let body = Json(json!({ "error": self.to_string() })); (status, body).into_response() } } impl From for OrchestratorError { fn from(e: anyhow::Error) -> Self { Self::Internal(e.to_string()) } }