1#[derive(Debug, thiserror::Error)]
2pub enum AppError {
3 #[error(transparent)]
4 Orm(#[from] che_orm::OrmError),
5 #[error(transparent)]
6 Io(#[from] std::io::Error),
7 #[error(transparent)]
8 Toml(#[from] toml::de::Error),
9 #[error(transparent)]
10 Json(#[from] serde_json::Error),
11 #[error("bad request: {0}")]
12 BadRequest(String),
13 #[error("not found")]
14 NotFound,
15 #[error("unauthorized: {0}")]
16 Unauthorized(String),
17 #[error("forbidden: {0}")]
18 Forbidden(String),
19 #[error("conflict: {0}")]
20 Conflict(String),
21}
22
23pub type AppResult<T> = Result<T, AppError>;
24
25impl axum::response::IntoResponse for AppError {
26 fn into_response(self) -> axum::response::Response {
27 match self {
28 Self::NotFound => (
29 axum::http::StatusCode::NOT_FOUND,
30 axum::Json(serde_json::json!({ "detail": "not found" })),
31 )
32 .into_response(),
33 Self::Unauthorized(detail) => (
34 axum::http::StatusCode::UNAUTHORIZED,
35 axum::Json(serde_json::json!({ "detail": detail })),
36 )
37 .into_response(),
38 Self::Forbidden(detail) => (
39 axum::http::StatusCode::FORBIDDEN,
40 axum::Json(serde_json::json!({ "detail": detail })),
41 )
42 .into_response(),
43 Self::Conflict(detail) => (
44 axum::http::StatusCode::CONFLICT,
45 axum::Json(serde_json::json!({ "detail": detail })),
46 )
47 .into_response(),
48 Self::BadRequest(detail) => (
49 axum::http::StatusCode::BAD_REQUEST,
50 axum::Json(serde_json::json!({ "detail": detail })),
51 )
52 .into_response(),
53 error => (
54 axum::http::StatusCode::INTERNAL_SERVER_ERROR,
55 axum::Json(serde_json::json!({ "detail": error.to_string() })),
56 )
57 .into_response(),
58 }
59 }
60}