use axum::response::IntoResponse;
#[derive(Clone)]
pub struct Error {
pub debug: String,
pub message: String,
}
pub type Result<T> = std::result::Result<T, Error>;
impl Error {
pub fn new(message: impl ToString) -> Self {
Self {
message: message.to_string(),
debug: message.to_string(),
}
}
#[tracing::instrument(skip(message))]
pub fn wrap<E: std::error::Error>(message: impl ToString) -> impl FnOnce(E) -> Self {
let message = message.to_string();
move |error| Self {
message,
debug: format!("{error:?}"),
}
}
#[tracing::instrument(skip(response))]
pub async fn wrap_response(response: impl IntoResponse) -> Self {
let error = axum::body::to_bytes(response.into_response().into_body(), 1024)
.await
.map(|bytes| String::from_utf8_lossy(&bytes).to_string())
.map_err(Self::wrap("Unable to get response body to string"))
.map(Self::new);
match error {
Ok(err) => err,
Err(err) => err,
}
}
}
impl std::error::Error for Error {}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.message)
}
}
impl std::fmt::Debug for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.debug)
}
}
impl From<quokka_templating::Error> for Error {
fn from(value: quokka_templating::Error) -> Self {
Self {
debug: format!("{value:?}"),
message: value.to_string(),
}
}
}