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

anyhow-based error handling example should be advertised more broadly #2046

Closed
1 task done
vi opened this issue Jun 18, 2023 · 4 comments
Closed
1 task done

anyhow-based error handling example should be advertised more broadly #2046

vi opened this issue Jun 18, 2023 · 4 comments
Labels
E-easy Call for participation: Experience needed to fix: Easy / not much E-help-wanted Call for participation: Help is requested to fix this issue. T-docs Topic: documentation

Comments

@vi
Copy link
Contributor

vi commented Jun 18, 2023

  • I have looked for existing issues (including closed) about this

Feature Request

Motivation

Primary place where I end up when looking how to make fallible handlers is axum::error_handling module documentation page. It even contains mentions of "anyhow" in "Routing to fallible services" section, but it is not clear how just upgrade existing infallible handlers to anyhow-fallible ones.

Fortunately, there is already an example of how to wrap Anyhow errors for Axum consumption (even with a properly working ? operator), but it is not obvious how to get there.

Proposal

Include a link to anyhow-error-response in axum::error_handling module documentation, e.g. in "axum’s error handling model" section.

Alternatives

  • Inline the example of anyhow-wrapping AppError directly in the docs (instead of a link).
  • Maybe also introduce "Routing to fallible handlers" section after "model", before "fallible services" which is oriented on practical approaches of making fallible handlers, with only theoretical things retained in the "model" section.
@davidpdrsn
Copy link
Member

Like described in the error handling docs axum does not consider a handler returning Result<_, _> as fallible. Thats just something that'll be converted into a response. Only service level errors are considered errors since they don't send back a response at all and instead hyper terminates the connection.

With that said I think it's fine to link to the anyhow example from the docs. Do you want to submit a PR for that?

@davidpdrsn davidpdrsn added E-easy Call for participation: Experience needed to fix: Easy / not much E-help-wanted Call for participation: Help is requested to fix this issue. T-docs Topic: documentation labels Jun 20, 2023
@vi
Copy link
Contributor Author

vi commented Jun 20, 2023

Like described in the error handling docs axum does not consider a handler returning Result<_, _> as fallible. Thats just something that'll be converted into a response. Only service level errors are considered errors since they don't send back a response at all and instead hyper terminates the connection.

Do I understand that there can be two meanings of the word "fallible" in this context:

  • "fallible service", in tower_service::Service<Error != Infallible> sense. This is banned for axum::handler::Handlers, as they do not have error type inside. So "real", hard errors should be already handled within a Hander before it reaches the router.
  • "fallible endpoint/handler", meaning request that can fail in user code (because it uses fallible operations inside and contains ? operators). Router does not "see" those errors, as they are converted to responses just like successful outcomes.

With that said I think it's fine to link to the anyhow example from the docs. Do you want to submit a PR for that?

Maybe, but I want to ensure I understand it properly myself first.
Shall it be a minimal change to just include the link or more substantial one to make the doc easier to understand for beginners?

@davidpdrsn
Copy link
Member

I don't think we should document returning Result from handlers in the "error handling" module as that module is about service level errors.

I think we should just link to the anyhow example.

@davidpdrsn
Copy link
Member

Fixed in #2049

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E-easy Call for participation: Experience needed to fix: Easy / not much E-help-wanted Call for participation: Help is requested to fix this issue. T-docs Topic: documentation
Projects
None yet
Development

No branches or pull requests

2 participants