use axum::http::{HeaderValue, StatusCode, header};
use axum::response::{IntoResponse, Response};
#[derive(Debug)]
pub struct ApiError {
status: StatusCode,
kind: &'static str,
code: &'static str,
message: String,
retry_after: Option<u64>,
}
impl ApiError {
pub fn new(status: StatusCode, kind: &'static str, code: &'static str, message: impl Into<String>) -> Self {
Self { status, kind, code, message: message.into(), retry_after: None }
}
pub fn bad_request(code: &'static str, message: impl Into<String>) -> Self {
Self::new(StatusCode::BAD_REQUEST, "invalid_request_error", code, message)
}
pub fn unauthorized() -> Self {
Self::new(StatusCode::UNAUTHORIZED, "authentication_error", "invalid_api_key", "missing or invalid bearer token")
}
pub fn forbidden(code: &'static str, message: impl Into<String>) -> Self {
Self::new(StatusCode::FORBIDDEN, "permission_error", code, message)
}
pub fn busy(retry_after: u64) -> Self {
Self {
retry_after: Some(retry_after),
..Self::new(StatusCode::SERVICE_UNAVAILABLE, "rate_limit_error", "server_busy", "the server is busy; retry shortly")
}
}
pub fn internal(message: impl Into<String>) -> Self {
Self::new(StatusCode::INTERNAL_SERVER_ERROR, "server_error", "internal_error", message)
}
}
impl From<loqui::Error> for ApiError {
fn from(e: loqui::Error) -> Self {
if let loqui::Error::Disabled(what) = e {
return Self::new(
StatusCode::NOT_FOUND,
"invalid_request_error",
"not_enabled",
format!("{what} is not enabled on this server"),
);
}
if e.is_client_error() {
return Self::bad_request("invalid_request", e.to_string());
}
tracing::error!(error = %e, "request failed");
Self::internal("the request could not be completed; see the server log")
}
}
impl IntoResponse for ApiError {
fn into_response(self) -> Response {
let body = serde_json::json!({
"error": { "message": self.message, "type": self.kind, "code": self.code }
});
let mut response = (self.status, axum::Json(body)).into_response();
if self.status == StatusCode::UNAUTHORIZED {
response.headers_mut().insert(header::WWW_AUTHENTICATE, HeaderValue::from_static("Bearer"));
}
if let Some(secs) = self.retry_after {
response.headers_mut().insert(header::RETRY_AFTER, HeaderValue::from(secs));
}
response
}
}