1use axum::{http::StatusCode, response::IntoResponse};
2
3pub type Result<T, E = Error> = std::result::Result<T, E>;
4
5pub enum Error {
6 Http(StatusCode, String),
7 Database(sqlx::Error),
8 Database2(sea_orm::error::DbErr),
9 Internal(anyhow::Error),
10}
11
12impl From<anyhow::Error> for Error {
13 fn from(error: anyhow::Error) -> Self {
14 Self::Internal(error)
15 }
16}
17
18impl From<sqlx::Error> for Error {
19 fn from(error: sqlx::Error) -> Self {
20 Self::Database(error)
21 }
22}
23
24impl From<sea_orm::error::DbErr> for Error {
25 fn from(error: sea_orm::error::DbErr) -> Self {
26 Self::Database2(error)
27 }
28}
29
30impl From<axum::Error> for Error {
31 fn from(error: axum::Error) -> Self {
32 Self::Internal(error.into())
33 }
34}
35
36impl From<hyper::Error> for Error {
37 fn from(error: hyper::Error) -> Self {
38 Self::Internal(error.into())
39 }
40}
41
42impl From<serde_json::Error> for Error {
43 fn from(error: serde_json::Error) -> Self {
44 Self::Internal(error.into())
45 }
46}
47
48impl IntoResponse for Error {
49 fn into_response(self) -> axum::response::Response {
50 match self {
51 Error::Http(code, message) => (code, message).into_response(),
52 Error::Database(error) => {
53 (StatusCode::INTERNAL_SERVER_ERROR, format!("{}", &error)).into_response()
54 }
55 Error::Database2(error) => {
56 (StatusCode::INTERNAL_SERVER_ERROR, format!("{}", &error)).into_response()
57 }
58 Error::Internal(error) => {
59 (StatusCode::INTERNAL_SERVER_ERROR, format!("{}", &error)).into_response()
60 }
61 }
62 }
63}
64
65impl std::fmt::Debug for Error {
66 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
67 match self {
68 Error::Http(code, message) => (code, message).fmt(f),
69 Error::Database(error) => error.fmt(f),
70 Error::Database2(error) => error.fmt(f),
71 Error::Internal(error) => error.fmt(f),
72 }
73 }
74}
75
76impl std::fmt::Display for Error {
77 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
78 match self {
79 Error::Http(code, message) => write!(f, "{code}: {message}"),
80 Error::Database(error) => error.fmt(f),
81 Error::Database2(error) => error.fmt(f),
82 Error::Internal(error) => error.fmt(f),
83 }
84 }
85}
86
87impl std::error::Error for Error {}