site-backend/src/error.rs

52 lines
1.2 KiB
Rust

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),
}
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),
}
}
}
impl Error for ServerError {}
impl IntoResponse for ServerError {
fn into_response(self) -> Response {
match self {
ServerError::IO(_) => StatusCode::INTERNAL_SERVER_ERROR.into_response(),
ServerError::Axum(_) => StatusCode::INTERNAL_SERVER_ERROR.into_response(),
}
}
}
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)
}
}
impl From<tokio::task::JoinError> for ServerError {
fn from(err: tokio::task::JoinError) -> Self {
ServerError::IO(err.into())
}
}