2025-02-26 13:10:45 -08:00
|
|
|
use std::fmt::{self, Display};
|
|
|
|
|
2025-02-26 13:10:50 -08:00
|
|
|
use axum::http::StatusCode;
|
2025-04-11 23:03:39 -07:00
|
|
|
use axum::response::{IntoResponse, Response};
|
2025-03-12 23:12:13 -07:00
|
|
|
use validator::ValidationErrors;
|
2025-02-26 13:10:45 -08:00
|
|
|
|
2025-03-14 13:04:57 -07:00
|
|
|
/// Custom error type that maps to appropriate HTTP responses.
|
2025-02-26 13:10:50 -08:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum AppError {
|
2025-02-26 13:10:45 -08:00
|
|
|
InternalServerError(anyhow::Error),
|
2025-04-11 23:06:27 -07:00
|
|
|
Forbidden(String),
|
|
|
|
NotFound(String),
|
|
|
|
BadRequest(String),
|
|
|
|
TooManyRequests(String),
|
2025-02-26 13:10:45 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AppError {
|
2025-03-12 23:12:13 -07:00
|
|
|
pub fn from_validation_errors(errs: ValidationErrors) -> Self {
|
2025-03-14 13:04:57 -07:00
|
|
|
// TODO: customize validation errors formatting
|
2025-04-11 23:06:27 -07:00
|
|
|
Self::BadRequest(serde_json::to_string(&errs).unwrap_or("validation error".to_string()))
|
2025-03-12 23:12:13 -07:00
|
|
|
}
|
2025-02-26 13:10:50 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl IntoResponse for AppError {
|
|
|
|
fn into_response(self) -> Response {
|
|
|
|
match self {
|
|
|
|
Self::InternalServerError(err) => {
|
2025-02-26 13:10:47 -08:00
|
|
|
tracing::error!("Application error: {:?}", err);
|
2025-02-26 13:10:50 -08:00
|
|
|
(StatusCode::INTERNAL_SERVER_ERROR, "Something went wrong").into_response()
|
|
|
|
}
|
2025-04-11 23:06:27 -07:00
|
|
|
Self::Forbidden(client_message) => {
|
2025-02-26 13:10:47 -08:00
|
|
|
tracing::info!("Forbidden: {}", client_message);
|
|
|
|
(StatusCode::FORBIDDEN, client_message).into_response()
|
|
|
|
}
|
2025-04-11 23:06:27 -07:00
|
|
|
Self::NotFound(client_message) => {
|
2025-02-26 13:10:45 -08:00
|
|
|
tracing::info!("Not found: {}", client_message);
|
|
|
|
(StatusCode::NOT_FOUND, client_message).into_response()
|
|
|
|
}
|
2025-04-11 23:06:27 -07:00
|
|
|
Self::TooManyRequests(client_message) => {
|
2025-03-10 14:52:02 -07:00
|
|
|
// Debug level so that if this is from a runaway loop, it won't
|
|
|
|
// overwhelm server logs
|
|
|
|
tracing::debug!("Too many requests: {}", client_message);
|
|
|
|
(StatusCode::TOO_MANY_REQUESTS, client_message).into_response()
|
|
|
|
}
|
2025-04-11 23:06:27 -07:00
|
|
|
Self::BadRequest(client_message) => {
|
2025-02-26 13:10:45 -08:00
|
|
|
tracing::info!("Bad user input: {}", client_message);
|
|
|
|
(StatusCode::BAD_REQUEST, client_message).into_response()
|
|
|
|
}
|
2025-02-26 13:10:50 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-14 13:04:57 -07:00
|
|
|
// Easily convert semi-arbitrary errors to InternalServerError
|
2025-02-26 13:10:50 -08:00
|
|
|
impl<E> From<E> for AppError
|
|
|
|
where
|
|
|
|
E: Into<anyhow::Error>,
|
|
|
|
{
|
|
|
|
fn from(err: E) -> Self {
|
|
|
|
Self::InternalServerError(Into::<anyhow::Error>::into(err))
|
|
|
|
}
|
|
|
|
}
|
2025-02-26 13:10:45 -08:00
|
|
|
|
|
|
|
impl Display for AppError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
AppError::InternalServerError(inner) => inner.fmt(f),
|
2025-04-11 23:06:27 -07:00
|
|
|
AppError::Forbidden(client_message) => {
|
2025-02-26 13:10:45 -08:00
|
|
|
write!(f, "ForbiddenError: {}", client_message)
|
|
|
|
}
|
2025-04-11 23:06:27 -07:00
|
|
|
AppError::NotFound(client_message) => {
|
2025-02-26 13:10:45 -08:00
|
|
|
write!(f, "NotFoundError: {}", client_message)
|
|
|
|
}
|
2025-04-11 23:06:27 -07:00
|
|
|
AppError::BadRequest(client_message) => {
|
2025-02-26 13:10:45 -08:00
|
|
|
write!(f, "BadRequestError: {}", client_message)
|
|
|
|
}
|
2025-04-11 23:06:27 -07:00
|
|
|
AppError::TooManyRequests(client_message) => {
|
2025-03-10 14:52:02 -07:00
|
|
|
write!(f, "TooManyRequestsError: {}", client_message)
|
|
|
|
}
|
2025-02-26 13:10:45 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|