lyrs/src/error.rs

28 lines
767 B
Rust

use crate::prelude::*;
use axum::{http::StatusCode, response::IntoResponse};
#[derive(Debug)]
pub struct Error(pub Box<dyn std::error::Error>);
impl IntoResponse for Error {
fn into_response(self) -> axum::http::Response<axum::body::Body> {
error!("webserver error: {:?}", self.0);
(
StatusCode::INTERNAL_SERVER_ERROR,
format!("internal server error: {}", self.0),
)
.into_response()
}
}
// This enables using `?` on functions that return `Result<_, anyhow::Error>`
// to turn them into `Result<_, AppError>`. That way you don't need to do that
// manually.
impl<E> From<E> for Error
where
E: Into<Box<dyn std::error::Error>>,
{
fn from(err: E) -> Self {
Self(err.into())
}
}