XciD's picture
XciD HF Staff
Initial Rust port of anthropic-messages-proxy
4f704aa
use axum::{
http::StatusCode,
response::{IntoResponse, Response},
Json,
};
use thiserror::Error;
use crate::types::anthropic::{Error as AnthropicError, InnerError};
#[derive(Error, Debug)]
pub enum AppError {
#[error("Request error: {0}")]
RequestError(String),
#[error("Upstream error: {0} - {1}")]
UpstreamError(u16, String),
#[error("Invalid request: {0}")]
InvalidRequest(String),
#[allow(dead_code)]
#[error("Internal error: {0}")]
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()
}
}