use axum::{
http::StatusCode,
response::{IntoResponse, Response},
};
use axum_extra::{headers::ContentType, TypedHeader};
use crate::serialize_error;
pub type Result<T> = std::result::Result<T, Error>;
pub struct Error(pub StatusCode, pub anyhow::Error);
impl IntoResponse for Error {
fn into_response(self) -> Response {
(
self.0,
TypedHeader(ContentType::json()),
serialize_error(&self.1),
)
.into_response()
}
}
impl<E> From<E> for Error
where
E: Into<anyhow::Error>,
{
fn from(err: E) -> Self {
Self(StatusCode::INTERNAL_SERVER_ERROR, err.into())
}
}
pub trait AddStatusCode: Into<anyhow::Error> {
fn status_code(self, status_code: StatusCode) -> Error {
Error(status_code, self.into())
}
}
impl<E> AddStatusCode for E where E: Into<anyhow::Error> {}