2026-04-25 16:41:57 -05:00
|
|
|
use axum::{
|
|
|
|
|
http::StatusCode,
|
|
|
|
|
response::{IntoResponse, Response},
|
|
|
|
|
Json,
|
|
|
|
|
};
|
|
|
|
|
use thiserror::Error;
|
|
|
|
|
|
|
|
|
|
#[derive(Error, Debug)]
|
|
|
|
|
pub enum AppError {
|
|
|
|
|
#[error("Unauthorized")]
|
|
|
|
|
Unauthorized,
|
|
|
|
|
|
|
|
|
|
#[error("Not found")]
|
|
|
|
|
NotFound,
|
|
|
|
|
|
|
|
|
|
#[error("Bad request: {0}")]
|
|
|
|
|
BadRequest(String),
|
|
|
|
|
|
|
|
|
|
#[error("Internal error: {0}")]
|
|
|
|
|
Internal(#[from] anyhow::Error),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl IntoResponse for AppError {
|
|
|
|
|
fn into_response(self) -> Response {
|
|
|
|
|
let (status, message) = match &self {
|
2026-04-25 16:48:14 -05:00
|
|
|
AppError::Unauthorized => (StatusCode::UNAUTHORIZED, self.to_string()),
|
|
|
|
|
AppError::NotFound => (StatusCode::NOT_FOUND, self.to_string()),
|
|
|
|
|
AppError::BadRequest(msg) => (StatusCode::BAD_REQUEST, msg.clone()),
|
|
|
|
|
AppError::Internal(_) => {
|
2026-04-25 16:41:57 -05:00
|
|
|
tracing::error!("{}", self);
|
|
|
|
|
(StatusCode::INTERNAL_SERVER_ERROR, "Internal server error".into())
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
(status, Json(serde_json::json!({ "error": message }))).into_response()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<sqlx::Error> for AppError {
|
|
|
|
|
fn from(e: sqlx::Error) -> Self {
|
|
|
|
|
AppError::Internal(anyhow::anyhow!(e))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<jsonwebtoken::errors::Error> for AppError {
|
2026-04-25 16:48:14 -05:00
|
|
|
fn from(_e: jsonwebtoken::errors::Error) -> Self {
|
2026-04-25 16:41:57 -05:00
|
|
|
AppError::Unauthorized
|
|
|
|
|
}
|
|
|
|
|
}
|