use anyhow::anyhow;
use std::fmt::Display;
use axum::response::{IntoResponse, Response};
use hyper::StatusCode;
#[derive(Debug)]
pub struct AppError(anyhow::Error);
impl Display for AppError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl IntoResponse for AppError {
fn into_response(self) -> Response {
let err = self.0;
tracing::error!(%err, "error");
(StatusCode::INTERNAL_SERVER_ERROR, format!("ERROR: {}", &err)).into_response()
}
}
impl<E> From<E> for AppError
where
E: Into<anyhow::Error>,
{
fn from(err: E) -> Self {
Self(err.into())
}
}
impl AppError {
pub fn new<T: std::error::Error + Send + Sync + 'static>(err: T) -> Self {
Self(anyhow!(err))
}
}