Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jplatte opened this issue Mar 22, 2024 · 0 comments
Labels
A-axum C-feature-request Category: A feature request, i.e: not implemented / a PR. I-needs-decision Issues in need of decision.

Comments

@jplatte
Copy link
Member

jplatte commented Mar 22, 2024

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?

@jplatte jplatte added C-feature-request Category: A feature request, i.e: not implemented / a PR. I-needs-decision Issues in need of decision. A-axum labels Mar 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-axum C-feature-request Category: A feature request, i.e: not implemented / a PR. I-needs-decision Issues in need of decision.
Projects
None yet
Development

No branches or pull requests

1 participant