anyhow Result
support
#1941
-
Hi, I'm wondering if it's possible to support the struct AppError(anyhow::Error);
// Tell axum how to convert `AppError` into a response.
impl IntoResponse for AppError {
fn into_response(self) -> Response {
(
StatusCode::INTERNAL_SERVER_ERROR,
format!("Something went wrong: {}", self.0),
)
.into_response()
}
} As far as I understand, we need it, because Rust forbids to implement the trait Is there a way around this limitation? Or would it be feasible to implement direct anyhow support into axum behind a feature flag? Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 7 replies
-
Technically it is possible but I think it would be a footgun and leads to many questions. How should it be turned into a response? As text, json, xml, yaml? What should the formatting be like? Therefore we recommend you make a wrapper like shown here https://github.com/tokio-rs/axum/blob/main/examples/anyhow-error-response/src/main.rs |
Beta Was this translation helpful? Give feedback.
-
Thank you. True, if that trait would be implemented in axum, I don't have the control anymore how to turn |
Beta Was this translation helpful? Give feedback.
-
How about responding with 500 and if #[cfg(debug_assertions)] => Display/Debug in the body, while also logging/tracing? Current anyhow example is pretty verbose and it seems like an easy opportunity to improve usability |
Beta Was this translation helpful? Give feedback.
Technically it is possible but I think it would be a footgun and leads to many questions. How should it be turned into a response? As text, json, xml, yaml? What should the formatting be like?
anyhow::Error
s are also likely to contain private details about the application that you don't want to expose to clients.Therefore we recommend you make a wrapper like shown here https://github.com/tokio-rs/axum/blob/main/examples/anyhow-error-response/src/main.rs