use axum::{http::StatusCode, response::IntoResponse}; // Based off https://github.com/tokio-rs/axum/blob/main/examples/anyhow-error-response/src/main.rs; pub enum AppError { // Generic error from ? Err(anyhow::Error), // HandledErr(StatusCode, &'static str), } // Let axum know how to turn our error into a response impl IntoResponse for AppError { fn into_response(self) -> axum::response::Response { match self { // AppError::Handled(status, text) => (status, text).into_response(), Self::Err(anyhow) => ( StatusCode::INTERNAL_SERVER_ERROR, format!("Something went wrong! {anyhow}"), ) .into_response(), Self::HandledErr(code, text) => (code, text).into_response(), } } } // Convert Result<_, anyhow::Error> into Result<_, AppError> impl From for AppError where E: Into, { fn from(err: E) -> Self { Self::Err(err.into()) } }