shoutdotdev/src/app_error.rs

84 lines
2.9 KiB
Rust
Raw Normal View History

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;
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),
ForbiddenError(String),
2025-02-26 13:10:45 -08:00
NotFoundError(String),
BadRequestError(String),
TooManyRequestsError(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-03-12 23:12:13 -07:00
Self::BadRequestError(
serde_json::to_string(&errs).unwrap_or("validation error".to_string()),
)
}
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()
}
Self::ForbiddenError(client_message) => {
tracing::info!("Forbidden: {}", client_message);
(StatusCode::FORBIDDEN, client_message).into_response()
}
2025-02-26 13:10:45 -08:00
Self::NotFoundError(client_message) => {
tracing::info!("Not found: {}", client_message);
(StatusCode::NOT_FOUND, client_message).into_response()
}
Self::TooManyRequestsError(client_message) => {
// 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-02-26 13:10:45 -08:00
Self::BadRequestError(client_message) => {
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),
AppError::ForbiddenError(client_message) => {
write!(f, "ForbiddenError: {}", client_message)
}
AppError::NotFoundError(client_message) => {
write!(f, "NotFoundError: {}", client_message)
}
AppError::BadRequestError(client_message) => {
write!(f, "BadRequestError: {}", client_message)
}
AppError::TooManyRequestsError(client_message) => {
write!(f, "TooManyRequestsError: {}", client_message)
}
2025-02-26 13:10:45 -08:00
}
}
}