panorama/crates/panorama-daemon/src/error.rs

35 lines
872 B
Rust
Raw Normal View History

2024-05-25 10:04:05 +00:00
use axum::{
http::StatusCode,
response::{IntoResponse, Response},
};
2024-05-25 11:45:23 +00:00
2024-05-25 10:04:05 +00:00
pub type AppResult<T, E = AppError> = std::result::Result<T, E>;
// Make our own error that wraps `anyhow::Error`.
2024-05-27 20:56:39 +00:00
#[derive(Debug)]
2024-05-25 10:04:05 +00:00
pub struct AppError(miette::Report);
// Tell axum how to convert `AppError` into a response.
impl IntoResponse for AppError {
fn into_response(self) -> Response {
2024-05-25 10:37:25 +00:00
eprintln!("Encountered error: {}", self.0);
2024-05-25 16:13:07 +00:00
eprintln!("{:?}", self.0);
2024-05-25 10:04:05 +00:00
(
StatusCode::INTERNAL_SERVER_ERROR,
format!("Something went wrong: {}", 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 AppError
where
E: Into<miette::Report>,
{
fn from(err: E) -> Self {
Self(err.into())
}
}