use axum::{http::StatusCode, response::IntoResponse}; pub type Result = std::result::Result; pub enum Error { Http(StatusCode, String), Database(sqlx::Error), Database2(sea_orm::error::DbErr), Internal(anyhow::Error), } impl From for Error { fn from(error: anyhow::Error) -> Self { Self::Internal(error) } } impl From for Error { fn from(error: sqlx::Error) -> Self { Self::Database(error) } } impl From for Error { fn from(error: sea_orm::error::DbErr) -> Self { Self::Database2(error) } } impl From for Error { fn from(error: axum::Error) -> Self { Self::Internal(error.into()) } } impl From for Error { fn from(error: hyper::Error) -> Self { Self::Internal(error.into()) } } impl From for Error { fn from(error: serde_json::Error) -> Self { Self::Internal(error.into()) } } impl IntoResponse for Error { fn into_response(self) -> axum::response::Response { match self { Error::Http(code, message) => (code, message).into_response(), Error::Database(error) => { (StatusCode::INTERNAL_SERVER_ERROR, format!("{}", &error)).into_response() } Error::Database2(error) => { (StatusCode::INTERNAL_SERVER_ERROR, format!("{}", &error)).into_response() } Error::Internal(error) => { (StatusCode::INTERNAL_SERVER_ERROR, format!("{}", &error)).into_response() } } } } impl std::fmt::Debug for Error { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Error::Http(code, message) => (code, message).fmt(f), Error::Database(error) => error.fmt(f), Error::Database2(error) => error.fmt(f), Error::Internal(error) => error.fmt(f), } } } impl std::fmt::Display for Error { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Error::Http(code, message) => write!(f, "{code}: {message}"), Error::Database(error) => error.fmt(f), Error::Database2(error) => error.fmt(f), Error::Internal(error) => error.fmt(f), } } } impl std::error::Error for Error {}