tokio-rs / axum

Ergonomic and modular web framework built with Tokio, Tower, and Hyper

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Consider making it easier to construct ErrorResponse from a non-IntoResponse error

jplatte opened this issue · comments

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

  1. Expose a free function, same thing but less verbose

    try_thing().map_err(internal_server_error)?;
  2. 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?