Skip to content

Commit a151aac

Browse files
committed
Improve some error messages behind a nightly-error-messages feature
flag This uses the nightly only `rustc_on_unimplemented` attribute to improve some error messages when users try to use invalid handler functions. This should be seen as prove of concept, not as full solution for all potential error cases. The underlying feature is currently marked as permanently unstable, but I'm working on getting this specific attribute (or an attribute with different name, similar functionality) ready to work on a stable compiler.
1 parent d5d076d commit a151aac

File tree

6 files changed

+23
-0
lines changed

6 files changed

+23
-0
lines changed

axum-core/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,6 @@ futures-util = "0.3"
2727
hyper = "0.14"
2828
tokio = { version = "1.0", features = ["macros"] }
2929
tower-http = { version = "0.3.4", features = ["limit"] }
30+
31+
[features]
32+
nightly-error-messages = []

axum-core/src/extract/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ mod private {
3939
///
4040
/// [`axum::extract`]: https://docs.rs/axum/0.6.0-rc.2/axum/extract/index.html
4141
#[async_trait]
42+
#[cfg_attr(
43+
feature = "nightly-error-messages",
44+
rustc_on_unimplemented(
45+
on(
46+
_Self = "axum::http::Request<axum::body::Body>",
47+
message = "Request must always be the last argument to a handler function",
48+
label = "Move this argument to the end of the argument list",
49+
),
50+
note = "Function argument is no valid axum extractor. \nSee `https://docs.rs/axum/latest/axum/extract/index.html` for details",
51+
)
52+
)]
4253
pub trait FromRequestParts<S>: Sized {
4354
/// If the extractor fails it'll use this "rejection" type. A rejection is
4455
/// a kind of error that can be converted into a response.

axum-core/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![cfg_attr(feature = "nightly-error-messages", feature(rustc_attrs))]
12
//! Core types and traits for [`axum`].
23
//!
34
//! Libraries authors that want to provide [`FromRequest`] or [`IntoResponse`] implementations

axum/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ query = ["dep:serde_urlencoded"]
2525
tokio = ["dep:tokio", "hyper/server", "hyper/tcp", "tower/make"]
2626
tower-log = ["tower/log"]
2727
ws = ["tokio", "dep:tokio-tungstenite", "dep:sha-1", "dep:base64"]
28+
nightly-error-messages = ["axum-core/nightly-error-messages"]
2829

2930
# Required for intra-doc links to resolve correctly
3031
__private_docs = ["tower/full", "tower-http/full"]

axum/src/handler/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ pub(crate) use self::{
9999
/// {}
100100
/// ```
101101
#[doc = include_str!("../docs/debugging_handler_type_errors.md")]
102+
#[cfg_attr(
103+
feature = "nightly-error-messages",
104+
rustc_on_unimplemented(
105+
note = "Consider using `#[axum_macros::debug_handler]` to improve the error message"
106+
)
107+
)]
102108
pub trait Handler<T, S, B = Body>: Clone + Send + Sized + 'static {
103109
/// The type of future calling this handler returns.
104110
type Future: Future<Output = Response> + Send + 'static;

axum/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![cfg_attr(feature = "nightly-error-messages", feature(rustc_attrs))]
12
//! axum is a web application framework that focuses on ergonomics and modularity.
23
//!
34
//! # Table of contents

0 commit comments

Comments
 (0)