1

Not sure I understand why, but I've got a route and a piece of middleware that are arranged like so:

let app = Router::new()
    .route("/a-path", post(create_a_thing))
    .router_layer(middleware::from_fn(get_auth))
    // ...

The middleware inserts a value (a token):

use crate::models::Token;

pub async fn get_auth<B>(
    TypedHeader(auth): TypedHeader<Authorization<Bearer>>,
    mut request: Request<B>,
    next: Next<B>
) -> Result<impl IntoResponse, (StatusCode, String)> {
    // derive token from auth
    req.extensions_mut().insert(token); // token is a Token
    Ok(next.run(req).await)
}

I had hoped to consume this in my route:

use crate::models::Token;

pub async create_a_thing(
    Extension(token): Extension<Token>,
    Json(payload): Json<Payload>
) -> impl IntoResponse {
    // do a db insert query using token and payload...
    
    (StatusCode::CREATED, String::from(""))
}

I could've sworn I did the same thing in another app and it works there, but here I get the following error in main.rs where my Router is defined:

│error[E0277]: the trait bound `fn(Extension<Token>, axum::Json<Payload>) -> impl std::future::Future<Ou│
│tput = impl IntoResponse> {create}: Handler<_, _, _>` is not satisfied                                 │
│   --> src/main.rs:13:33                                                                               │
│    |                                                                                                  │
│13  | ...", post(create_a_thing))                                                             │
│    |       ---- ^^^^^^^^^^^^^^^^ the trait `Handler<_, _, _>` is not implemented for fn item `f│
│n(Extension<Token>, axum::Json<Payload>) -> impl std::future::Future<Output = impl IntoResponse> {creat│
│e}`                                                                                                    │
│    |       |                                                                                          │
│    |       required by a bound introduced by this call                                                │
│    |                                                                                                  │
│    = help: the following other types implement trait `Handler<T, S, B>`:                              │
│              <MethodRouter<S, B> as Handler<(), S, B>>                                                │
│              <axum::handler::Layered<L, H, T, S, B, B2> as Handler<T, S, B2>>                         │
│note: required by a bound in `post`                                                                    │
│   --> /path/to/my/project/.cargo/registry/src/github.com-1ecc6299db9ec823/axum-0.6.16/src/routing/method_routi│
│ng.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 m│
│acro-backtrace for more info)  

What am I missing?

cafce25
  • 15,907
  • 4
  • 25
  • 31
neezer
  • 19,720
  • 33
  • 121
  • 220
  • That's seems to be saying that post is a future, maybe you have to await it? – Ahmed Masud Apr 23 '23 at 18:13
  • Put a `#[axum::debug_handler]` on top of `create_a_thing()`. – Chayim Friedman Apr 23 '23 at 19:06
  • @ChayimFriedman Thanks, that helped immensely! Looks like `Token` doesn't implement `Clone` and that's the real issue. If you post an answer explaining the use of `debug_handler` to diagnose issues like these, I'd happily accept it! – neezer Apr 23 '23 at 20:29

0 Answers0