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

Better error message regarding the order of extractors #1913

Closed
1 task done
azzamsa opened this issue Apr 6, 2023 · 1 comment · Fixed by #2027
Closed
1 task done

Better error message regarding the order of extractors #1913

azzamsa opened this issue Apr 6, 2023 · 1 comment · Fixed by #2027

Comments

@azzamsa
Copy link
Contributor

azzamsa commented Apr 6, 2023

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

Feature Request

Is it possible for us to have a better error message telling us about the wrong order of the extractors?

Motivation

I spend hours digging into the error message I faced. It makes me try every possible scenario, including trying TypedMultipart third-party crate. But it yields the same error message.

pub async fn post(
    multipart: Multipart,
    ctx: Extension<Arc<ServerContext>>,
) -> Result<Json<model::TtlResponse>, crate::Error> {
error[E0277]: the trait bound `fn(axum::extract::Multipart, axum::Extension<std::sync::Arc<context::ServerContext>>) -> impl std::future::Future<Output = std::result::Result<axum::Json<ttl::model::TtlResponse>, errors::Error>> {ttl::query::post}: axum::handler::Handler<_, _, _>` is not satisfied
   --> src/routes.rs:54:34
    |
54  |         .route("/ttl/add/", post(ttl::query::post));
    |                             ---- ^^^^^^^^^^^^^^^^ the trait `axum::handler::Handler<_, _, _>` is not implemented for fn item `fn(axum::extract::Multipart, axum::Extension<std::sync::Arc<context::ServerContext>>) -> impl std::future::Future<Output = std::result::Result<axum::Json<ttl::model::TtlResponse>, errors::Error>> {ttl::query::post}`
    |                             |
    |                             required by a bound introduced by this call
    |
    = help: the following other types implement trait `axum::handler::Handler<T, S, B>`:
              <axum::handler::Layered<L, H, T, S, B, B2> as axum::handler::Handler<T, S, B2>>
              <axum::routing::MethodRouter<S, B> as axum::handler::Handler<(), S, B>>
note: required by a bound in `axum::routing::post`
   --> /var/home/user/.local/share/cargo/registry/src/github.com-1ecc6299db9ec823/axum-0.6.12/src/routing/method_routing.rs:407:1
    |
407 | top_level_handler_fn!(post, POST);
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `post`
    = note: this error originates in the macro `top_level_handler_fn` (in Nightly builds, run with -Z macro-backtrace for more info)

I can't solve the issue until I find the code from a project that uses Actix Multipart. Which put the multipart extractor to the last parameter.

pub async fn post(
-   ctx: Extension<Arc<ServerContext>>,
+    multipart: Multipart,
) -> Result<Json<model::TtlResponse>, crate::Error> {

Proposal

Make the error message better. More helpful. Regarding the order of the extractors. Tell that the order of the extractor is wrong.

Alternatives

  1. Put a more verbose warning here. I added (⚠️) so it is more noticeable.

image

  1. Add other extractors to multipart form example Because most people like me, start at the examples, not the docs. This way, the readers are able to distinguish the extractor order.
@davidpdrsn
Copy link
Member

I wish that was possible but sadly, it's not. The best we can do is https://docs.rs/axum/0.6.12/axum/attr.debug_handler.html which has special knowledge of extractors in axum but can't know about all extractors from the ecosystem.

If you know of ways to improve things further please open another issue or PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants