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

Make TypedPath support Result and Option #994

Closed
ahcodedthat opened this issue May 4, 2022 · 1 comment · Fixed by #1001
Closed

Make TypedPath support Result and Option #994

ahcodedthat opened this issue May 4, 2022 · 1 comment · Fixed by #1001
Labels
A-axum-extra C-feature-request Category: A feature request, i.e: not implemented / a PR.

Comments

@ahcodedthat
Copy link

Feature Request

Motivation

When path extraction fails, I want to send an empty 404 response with no error message. The default error message exposes implementation details of my application to clients, and axum normally sends status code 400 instead of 404 in this situation.

With ordinary extractors like Path, this is simple: take an Option<Path<_>> or Result<Path<_>, PathRejection> instead of simply Path<_>, and then handle the failure as needed. But this is not allowed when using TypedPath because Option and Result don't implement it:

error[E0277]: the trait bound `std::option::Option<MyTypedPathStruct>: TypedPath` is not satisfied
  --> src/main.rs:87:5
   |
87 |             .typed_get(my_handler_fn)
   |              ^^^^^^^^^ the trait `TypedPath` is not implemented for `std::option::Option<MyTypedPathStruct>`
   |
   = note: required because of the requirements on the impl of `FirstElementIs<std::option::Option<MyTypedPathStruct>>` for `(std::option::Option<MyTypedPathStruct>, Extension<Arc<web_axum::Context>>)`
note: required by a bound in `typed_get`
  --> …/.cargo/registry/src/github.com-1ecc6299db9ec823/axum-extra-0.3.0/src/routing/mod.rs:36:12
   |
36 |         T: FirstElementIs<P> + 'static,
   |            ^^^^^^^^^^^^^^^^^ required by this bound in `typed_get`

Proposal

Please implement TypedPath for Option and/or Result.

Alternatives

  • Adding a layer that removes all text/plain bodies from error responses, parses the error messages, adjusts status codes, and so on.
  • Not using TypedPath at all.
@davidpdrsn davidpdrsn added A-axum-extra C-feature-request Category: A feature request, i.e: not implemented / a PR. labels May 5, 2022
@davidpdrsn davidpdrsn changed the title Implement TypedPath for Result Make TypedPath support Result and Option May 5, 2022
@davidpdrsn
Copy link
Member

Sounds good! I'm not quite sure what needs to be done but I have hunch its more about adding new impls for FirstElementIs than TypedPath.

In the same vein I think we should consider adding a way to use a different rejection such that you don't need to use Option/Result in each handler. Perhaps something the impls From<PathRejection> for YourNewRejection. I think thats a separate feature though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-axum-extra C-feature-request Category: A feature request, i.e: not implemented / a PR.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants