Invoke async code in ResponseError trait method error_response
#2593
-
|
In order to handle errors in my route, I created a custom struct CustomError {
// ...
}On error, I'd like to invoke a async function: impl ResponseError for CustomError {
fn error_response(&self) -> HttpResponse {
render_template("error.html").await // <-- Not possible to call .await in a sync function
}
// ...
}Sadly Rust doesn't have async traits (without the In Any idea how to handle this? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
I recommend rendering a simple response in In the middleware, you can downcast A related example https://github.com/svenstaro/miniserve/blob/f40e7d4c66775c8f89564ee1b2bbe4b2d7cfc660/src/errors.rs#L112 |
Beta Was this translation helpful? Give feedback.
I recommend rendering a simple response in
ResponseErrorand then using a middleware for fully rendering the error page. With this approach you have the luxury of async as well as having HttpRequest.In the middleware, you can downcast
HttpResponse::error()to get the underlying error. You also have the option to pass arbitrary data fromResponseError::error_response()to the middleware usingHttpResponse::extensions().A related example https://github.com/svenstaro/miniserve/blob/f40e7d4c66775c8f89564ee1b2bbe4b2d7cfc660/src/errors.rs#L112