Info
This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: Axum handler not handling
I've been using Rust for around 3 years now, and I'm running into a problem with an Axum handler. If you could offer some guidance, it would be much appreciated.
use axum::{Router, extract::State, response::IntoResponse, routing::get};
use std::sync::Arc;
use crate::web::WebAppState;
pub fn debug_router(state: Arc<WebAppState>) -> Router<Arc<WebAppState>> {
Router::new()
.route("/seed_db", get(seed_db_handler))
.with_state(state)
}
#[axum::debug_handler]
pub async fn seed_db_handler(State(state): State<Arc<WebAppState>>) -> impl IntoResponse {
let res = db::seed_database(&state.repo.clone_inner()).await;
match res {
Ok(_) => "Database Seeded Successfully!".into_response(),
Err(e) => format!("Error Seeding Database: {:?}", e).into_response(),
}
}
When I try to compile this code I get this error:
error[E0277]: the trait bound `fn(axum::extract::State<Arc<WebAppState>>) -> impl futures_util::Future<Output = impl IntoResponse> {web::debug::seed_db_handler}: Handler<_, _>` is not satisfied
--> web\src\web\debug.rs:8:32
|
8 | .route("/seed_db", get(seed_db_handler))
| --- ^^^^^^^^^^^^^^^ the trait `Handler<_, _>` is not implemented for fn item `fn(axum::extract::State<Arc<WebAppState>>) -> impl futures_util::Future<Output = impl IntoResponse> {web::debug::seed_db_handler}`
| |
| required by a bound introduced by this call
|
= note: Consider using `#[axum::debug_handler]` to improve the error message
= help: the following other types implement trait `Handler<T, S>`:
`MethodRouter<S>` implements `Handler<(), S>`
`axum::handler::Layered<L, H, T, S>` implements `Handler<T, S>`
note: required by a bound in `axum::routing::get`
--> C:\Users\User\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\axum-0.8.4\src\routing\method_routing.rs:441:1
|
441 | top_level_handler_fn!(get, GET);
| ^^^^^^^^^^^^^^^^^^^^^^---^^^^^^
| | |
| | required by a bound in this function
| required by this bound in `get`
= note: this error originates in the macro `top_level_handler_fn` (in Nightly builds, run with -Z macro-backtrace for more info)
I've gone through the Handler trait requirements on docs.rs and I believe I'm using it correctly, but it still is not compiling. Also, the #[debug_handler]
macro is does not give any helpful guidance, the compiler output is the same with and without it.
This router is gated behind a #[cfg(debug_assertions)]
in my main axum setup, like this:
let router = Router::new().route( ... ).with_state(state);
#[cfg(debug_assertions)]
let router = router.nest("/debug", debug::debug_router(self.clone()));
Removing the #[cfg(debug_assertions)]
does not solve the problem.
rust-analyzer also gives an error for one of my other handlers with the same error. The thing is I don't believe I have even changed that handler since the code compiled last. This may just be a problem with r-a.
If you could offer some guidance, it would be much appreciated.
1 post - 1 participant
🏷️ Rust_feed