blob: d8aaf18bd07f71b5035931f08f3b32489f2fd141 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
use axum::{
http::StatusCode,
response::{IntoResponse, Response},
};
pub enum AppError {
BadRequest,
Conflict,
Database(sqlx::Error),
NotFound,
}
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,
sqlx::Error::RowNotFound => AppError::NotFound,
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
}
AppError::NotFound => StatusCode::NOT_FOUND,
}
.into_response()
}
}
|