use axum::response::IntoResponse;
use thiserror::Error;
use crate::rest::response::ApiResponse;
pub type RestResult<T> = Result<T, RestError>;
#[derive(Debug, Error)]
pub enum RestError {
#[error("unauthorized")]
Unauthorized,
#[error("request timed out")]
Timeout,
#[error("bad request: {0}")]
BadRequest(String),
#[error("internal error: {0}")]
Internal(String),
}
impl RestError {
pub fn code(&self) -> &'static str {
match self {
Self::Unauthorized => "UNAUTHORIZED",
Self::Timeout => "TIMEOUT",
Self::BadRequest(_) => "BAD_REQUEST",
Self::Internal(_) => "INTERNAL",
}
}
}
impl IntoResponse for RestError {
fn into_response(self) -> axum::response::Response {
ApiResponse::<()>::fail(self.code(), self.to_string()).into_response()
}
}