Skip to content

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

@jplatte

Description

@jplatte

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-axumC-feature-requestCategory: A feature request, i.e: not implemented / a PR.I-needs-decisionIssues in need of decision.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions