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

35 lines
870 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-06-21 20:43:19 +00:00
pub struct AppError(anyhow::Error);
2024-05-25 10:04:05 +00:00
// 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
2024-06-21 20:43:19 +00:00
E: Into<anyhow::Error>,
2024-05-25 10:04:05 +00:00
{
fn from(err: E) -> Self {
Self(err.into())
}
}