Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
| use axum::{ | |
| http::StatusCode, | |
| response::{IntoResponse, Response}, | |
| Json, | |
| }; | |
| use thiserror::Error; | |
| use crate::types::anthropic::{Error as AnthropicError, InnerError}; | |
| pub enum AppError { | |
| RequestError(String), | |
| UpstreamError(u16, String), | |
| InvalidRequest(String), | |
| InternalError(String), | |
| } | |
| impl IntoResponse for AppError { | |
| fn into_response(self) -> Response { | |
| let (status, error_type, message) = match &self { | |
| AppError::RequestError(msg) => (StatusCode::BAD_GATEWAY, "api_error", msg.clone()), | |
| AppError::UpstreamError(code, msg) => { | |
| let status = StatusCode::from_u16(*code).unwrap_or(StatusCode::INTERNAL_SERVER_ERROR); | |
| (status, "api_error", msg.clone()) | |
| } | |
| AppError::InvalidRequest(msg) => { | |
| (StatusCode::BAD_REQUEST, "invalid_request_error", msg.clone()) | |
| } | |
| AppError::InternalError(msg) => { | |
| (StatusCode::INTERNAL_SERVER_ERROR, "api_error", msg.clone()) | |
| } | |
| }; | |
| let error = AnthropicError { | |
| content_type: "error".to_string(), | |
| error: InnerError { | |
| error_type: error_type.to_string(), | |
| message, | |
| }, | |
| }; | |
| (status, Json(error)).into_response() | |
| } | |
| } | |