diff options
Diffstat (limited to 'src/error.rs')
| -rw-r--r-- | src/error.rs | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..8b4bb6d --- /dev/null +++ b/src/error.rs @@ -0,0 +1,33 @@ +use axum::{ + http::StatusCode, + response::{IntoResponse, Response}, +}; + +pub enum AppError { + BadRequest, + Conflict, + Database(sqlx::Error), +} + +impl From<sqlx::Error> for AppError { + fn from(e: sqlx::Error) -> Self { + match e { + sqlx::Error::Database(e) if e.is_unique_violation() => AppError::Conflict, + e => AppError::Database(e), + } + } +} + +impl IntoResponse for AppError { + fn into_response(self) -> Response { + match self { + AppError::BadRequest => StatusCode::BAD_REQUEST, + AppError::Conflict => StatusCode::CONFLICT, + AppError::Database(e) => { + tracing::error!("database error: {e}"); + StatusCode::INTERNAL_SERVER_ERROR + } + } + .into_response() + } +} |
