Consider making it easier to construct ErrorResponse from a non-IntoResponse error
jplatte opened this issue · comments
Jonas Platte commented
Feature Request
Motivation
People ask about short-circuiting from handler functions with non-IntoResponse
errors somewhat frequently.
We already have ErrorResponse
in axum::response
for returning different error types from a handler function conveniently. However, right now it is quite annoying to construct one from a non-IntoResponse
error type (see alternatives).
Proposal
Add
impl ErrorResponse {
fn internal_server_error(message: impl Display) -> Self {
(StatusCode::INTERNAL_SERVER_ERROR, message.to_string()).into()
}
}
to allow users to write
try_thing().map_err(ErrorResponse::internal_server_error)?;
Alternatives
-
Expose a free function, same thing but less verbose
try_thing().map_err(internal_server_error)?;
-
Do nothing. One can already do
try_thing().map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
Open questions
Should the internal_server_error
function log the error as well?