Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
Rust
12345678910111213141516171819202122232425262728293031323334353637383940414243use axum::http::StatusCode;use axum::response::{IntoResponse, Response};use serde_json::json;
#[derive(Debug)]pub enum AppError { BadRequest(String), Unauthorized(String), NotFound(String), Internal(String),}
impl std::fmt::Display for AppError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { AppError::BadRequest(msg) => write!(f, "Bad request: {msg}"), AppError::Unauthorized(msg) => write!(f, "Unauthorized: {msg}"), AppError::NotFound(msg) => write!(f, "Not found: {msg}"), AppError::Internal(msg) => write!(f, "Internal error: {msg}"), } }}
impl IntoResponse for AppError { fn into_response(self) -> Response { let (status, message) = match &self { AppError::BadRequest(msg) => (StatusCode::BAD_REQUEST, msg.clone()), AppError::Unauthorized(msg) => (StatusCode::UNAUTHORIZED, msg.clone()), AppError::NotFound(msg) => (StatusCode::NOT_FOUND, msg.clone()), AppError::Internal(msg) => (StatusCode::INTERNAL_SERVER_ERROR, msg.clone()), };
let body = axum::Json(json!({ "error": message })); (status, body).into_response() }}
impl From<sqlx::Error> for AppError { fn from(err: sqlx::Error) -> Self { AppError::Internal(err.to_string()) }}