//! The HTTP-facing error type returned by fallible handlers. use axum::http::StatusCode; use axum::response::{IntoResponse, Response}; use thiserror::Error; /// Error returned by request handlers. #[derive(Debug, Error)] pub enum AppError { /// An internal server error wrapping any anyhow error. #[error("internal server error: {0}")] Internal(#[from] anyhow::Error), /// The request was malformed. #[error("{0}")] BadRequest(String), /// The request was unauthenticated or the session was invalid. #[error("unauthorized")] Unauthorized, /// The caller is authenticated but not allowed to do this. #[error("forbidden")] Forbidden, /// Nothing exists at this URL. #[error("not found")] NotFound, } impl IntoResponse for AppError { fn into_response(self) -> Response { let status = match &self { AppError::Internal(_) => StatusCode::INTERNAL_SERVER_ERROR, AppError::BadRequest(_) => StatusCode::BAD_REQUEST, AppError::Unauthorized => StatusCode::UNAUTHORIZED, AppError::Forbidden => StatusCode::FORBIDDEN, AppError::NotFound => StatusCode::NOT_FOUND, }; (status, self.to_string()).into_response() } }