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 Internal(anyhow::Error),
8}
9
10impl From<anyhow::Error> for Error {
11 fn from(error: anyhow::Error) -> Self {
12 Self::Internal(error)
13 }
14}
15
16impl From<sqlx::Error> for Error {
17 fn from(error: sqlx::Error) -> Self {
18 Self::Internal(error.into())
19 }
20}
21
22impl From<axum::Error> for Error {
23 fn from(error: axum::Error) -> Self {
24 Self::Internal(error.into())
25 }
26}
27
28impl From<hyper::Error> for Error {
29 fn from(error: hyper::Error) -> Self {
30 Self::Internal(error.into())
31 }
32}
33
34impl From<serde_json::Error> for Error {
35 fn from(error: serde_json::Error) -> Self {
36 Self::Internal(error.into())
37 }
38}
39
40impl IntoResponse for Error {
41 fn into_response(self) -> axum::response::Response {
42 match self {
43 Error::Http(code, message) => (code, message).into_response(),
44 Error::Internal(error) => {
45 (StatusCode::INTERNAL_SERVER_ERROR, format!("{}", &error)).into_response()
46 }
47 }
48 }
49}
50
51impl std::fmt::Debug for Error {
52 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
53 match self {
54 Error::Http(code, message) => (code, message).fmt(f),
55 Error::Internal(error) => error.fmt(f),
56 }
57 }
58}
59
60impl std::fmt::Display for Error {
61 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
62 match self {
63 Error::Http(code, message) => write!(f, "{code}: {message}"),
64 Error::Internal(error) => error.fmt(f),
65 }
66 }
67}
68
69impl std::error::Error for Error {}