2023-07-14 13:46:53 +02:00
|
|
|
use axum::http::StatusCode;
|
|
|
|
use axum::response::{IntoResponse, Response};
|
|
|
|
use std::error::Error;
|
|
|
|
use std::fmt;
|
|
|
|
use std::io;
|
|
|
|
|
|
|
|
pub type Result<T> = std::result::Result<T, ServerError>;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum ServerError {
|
|
|
|
IO(io::Error),
|
|
|
|
Axum(axum::Error),
|
2023-08-02 21:15:12 +02:00
|
|
|
Status(StatusCode),
|
2023-07-30 16:55:44 +02:00
|
|
|
Db(sea_orm::DbErr),
|
2023-07-14 13:46:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for ServerError {
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
ServerError::IO(err) => write!(fmt, "{}", err),
|
|
|
|
ServerError::Axum(err) => write!(fmt, "{}", err),
|
2023-08-02 21:15:12 +02:00
|
|
|
ServerError::Status(status) => write!(fmt, "{}", status),
|
2023-07-30 16:55:44 +02:00
|
|
|
ServerError::Db(err) => write!(fmt, "{}", err),
|
2023-07-14 13:46:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Error for ServerError {}
|
|
|
|
|
|
|
|
impl IntoResponse for ServerError {
|
|
|
|
fn into_response(self) -> Response {
|
2023-08-02 18:48:17 +02:00
|
|
|
tracing::error!("{:?}", self);
|
|
|
|
|
2023-07-14 13:46:53 +02:00
|
|
|
match self {
|
|
|
|
ServerError::IO(_) => StatusCode::INTERNAL_SERVER_ERROR.into_response(),
|
|
|
|
ServerError::Axum(_) => StatusCode::INTERNAL_SERVER_ERROR.into_response(),
|
2023-08-02 21:15:12 +02:00
|
|
|
ServerError::Status(status) => status.into_response(),
|
2023-07-30 16:55:44 +02:00
|
|
|
ServerError::Db(sea_orm::DbErr::RecordNotFound(_)) => StatusCode::NOT_FOUND.into_response(),
|
|
|
|
ServerError::Db(_) => StatusCode::INTERNAL_SERVER_ERROR.into_response(),
|
2023-07-14 13:46:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<io::Error> for ServerError {
|
|
|
|
fn from(err: io::Error) -> Self {
|
|
|
|
ServerError::IO(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<axum::Error> for ServerError {
|
|
|
|
fn from(err: axum::Error) -> Self {
|
|
|
|
ServerError::Axum(err)
|
|
|
|
}
|
|
|
|
}
|
2023-07-16 19:59:47 +02:00
|
|
|
|
|
|
|
impl From<tokio::task::JoinError> for ServerError {
|
|
|
|
fn from(err: tokio::task::JoinError) -> Self {
|
|
|
|
ServerError::IO(err.into())
|
|
|
|
}
|
|
|
|
}
|
2023-08-02 21:15:12 +02:00
|
|
|
|
|
|
|
impl From<StatusCode> for ServerError {
|
|
|
|
fn from(status: StatusCode) -> Self {
|
|
|
|
Self::Status(status)
|
|
|
|
}
|
|
|
|
}
|
2023-07-30 16:55:44 +02:00
|
|
|
|
|
|
|
impl From<sea_orm::DbErr> for ServerError {
|
|
|
|
fn from(err: sea_orm::DbErr) -> Self {
|
|
|
|
ServerError::Db(err)
|
|
|
|
}
|
|
|
|
}
|