mirror of
https://github.com/tokio-rs/axum.git
synced 2026-09-08 00:00:24 +02:00
Document debugging handler type errors with "axum-debug" (#372)
* Document debugging handler type errors with "axum-debug" * Apply suggestions from code review Co-authored-by: Jonas Platte <[email protected]> Co-authored-by: Jonas Platte <[email protected]>
This commit is contained in:
co-authored by
Jonas Platte
parent
d29c1f26c2
commit
5dc5a7cb6e
+39
@@ -5,6 +5,7 @@
|
|||||||
//! - [High level features](#high-level-features)
|
//! - [High level features](#high-level-features)
|
||||||
//! - [Compatibility](#compatibility)
|
//! - [Compatibility](#compatibility)
|
||||||
//! - [Handlers](#handlers)
|
//! - [Handlers](#handlers)
|
||||||
|
//! - [Debugging handler type errors](#debugging-handler-type-errors)
|
||||||
//! - [Routing](#routing)
|
//! - [Routing](#routing)
|
||||||
//! - [Precedence](#precedence)
|
//! - [Precedence](#precedence)
|
||||||
//! - [Matching multiple methods](#matching-multiple-methods)
|
//! - [Matching multiple methods](#matching-multiple-methods)
|
||||||
@@ -106,6 +107,41 @@
|
|||||||
//! }
|
//! }
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
|
//! ## Debugging handler type errors
|
||||||
|
//!
|
||||||
|
//! For a function to used as a handler it must implement the [`Handler`] trait.
|
||||||
|
//! axum provides blanket implementations for functions that:
|
||||||
|
//!
|
||||||
|
//! - Are `async fn`s.
|
||||||
|
//! - Take no more than 16 arguments that all implement [`FromRequest`].
|
||||||
|
//! - Returns something that implements [`IntoResponse`].
|
||||||
|
//! - If a closure is used it must implement `Clone + Send + Sync` and be
|
||||||
|
//! `'static`.
|
||||||
|
//! - Returns a future that is `Send`. The most common way to accidentally make a
|
||||||
|
//! future `!Send` is to hold a `!Send` type across an await.
|
||||||
|
//!
|
||||||
|
//! Unfortunately Rust gives poor error messages if you try to use a function
|
||||||
|
//! that doesn't quite match what's required by [`Handler`].
|
||||||
|
//!
|
||||||
|
//! You might get an error like this:
|
||||||
|
//!
|
||||||
|
//! ```not_rust
|
||||||
|
//! error[E0277]: the trait bound `fn(bool) -> impl Future {handler}: Handler<_, _>` is not satisfied
|
||||||
|
//! --> src/main.rs:13:44
|
||||||
|
//! |
|
||||||
|
//! 13 | let app = Router::new().route("/", get(handler));
|
||||||
|
//! | ^^^^^^^ the trait `Handler<_, _>` is not implemented for `fn(bool) -> impl Future {handler}`
|
||||||
|
//! |
|
||||||
|
//! ::: axum/src/handler/mod.rs:116:8
|
||||||
|
//! |
|
||||||
|
//! 116 | H: Handler<B, T>,
|
||||||
|
//! | ------------- required by this bound in `axum::handler::get`
|
||||||
|
//! ```
|
||||||
|
//!
|
||||||
|
//! This error doesn't tell you _why_ your function doesn't implement
|
||||||
|
//! [`Handler`]. It's possible to improve the error with the [`debug_handler`]
|
||||||
|
//! proc-macro from the [axum-debug] crate.
|
||||||
|
//!
|
||||||
//! # Routing
|
//! # Routing
|
||||||
//!
|
//!
|
||||||
//! Routing between handlers looks like this:
|
//! Routing between handlers looks like this:
|
||||||
@@ -1150,6 +1186,9 @@
|
|||||||
//! [`HeaderMap`]: http::header::HeaderMap
|
//! [`HeaderMap`]: http::header::HeaderMap
|
||||||
//! [`Request`]: http::Request
|
//! [`Request`]: http::Request
|
||||||
//! [customize-extractor-error]: https://github.com/tokio-rs/axum/blob/main/examples/customize-extractor-error/src/main.rs
|
//! [customize-extractor-error]: https://github.com/tokio-rs/axum/blob/main/examples/customize-extractor-error/src/main.rs
|
||||||
|
//! [axum-debug]: https://docs.rs/axum-debug
|
||||||
|
//! [`debug_handler`]: https://docs.rs/axum-debug/latest/axum_debug/attr.debug_handler.html
|
||||||
|
//! [`Handler`]: crate::handler::Handler
|
||||||
|
|
||||||
#![warn(
|
#![warn(
|
||||||
clippy::all,
|
clippy::all,
|
||||||
|
|||||||
Reference in New Issue
Block a user