diff --git a/axum/CHANGELOG.md b/axum/CHANGELOG.md index cace7db3..705b26ce 100644 --- a/axum/CHANGELOG.md +++ b/axum/CHANGELOG.md @@ -7,7 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 # Unreleased -- None +## Routing + +- **breaking:** Adding a `.route_layer` onto a `Router` or `MethodRouter` + without any routes will now result in a panic. Previously, this just did + nothing. [#1327] + +[#1327]: https://github.com/tokio-rs/axum/pull/1327 # 0.6.0-rc.1 (23. August, 2022) diff --git a/axum/src/routing/method_routing.rs b/axum/src/routing/method_routing.rs index e28a9e93..c3a69106 100644 --- a/axum/src/routing/method_routing.rs +++ b/axum/src/routing/method_routing.rs @@ -852,6 +852,7 @@ where } #[doc = include_str!("../docs/method_routing/route_layer.md")] + #[track_caller] pub fn route_layer(mut self, layer: L) -> MethodRouter where L: Layer>, @@ -859,6 +860,21 @@ where >>::Response: IntoResponse + 'static, >>::Future: Send + 'static, { + if self.get.is_none() + && self.head.is_none() + && self.delete.is_none() + && self.options.is_none() + && self.patch.is_none() + && self.post.is_none() + && self.put.is_none() + && self.trace.is_none() + { + panic!( + "Adding a route_layer before any routes is a no-op. \ + Add the routes you want the layer to apply to first." + ); + } + let layer_fn = |svc| { let svc = layer.layer(svc); let svc = MapResponseLayer::new(IntoResponse::into_response).layer(svc); diff --git a/axum/src/routing/mod.rs b/axum/src/routing/mod.rs index 8bcbbe22..3f75a968 100644 --- a/axum/src/routing/mod.rs +++ b/axum/src/routing/mod.rs @@ -352,6 +352,7 @@ where } #[doc = include_str!("../docs/routing/route_layer.md")] + #[track_caller] pub fn route_layer(self, layer: L) -> Self where L: Layer>, @@ -360,6 +361,13 @@ where >>::Error: Into + 'static, >>::Future: Send + 'static, { + if self.routes.is_empty() { + panic!( + "Adding a route_layer before any routes is a no-op. \ + Add the routes you want the layer to apply to first." + ); + } + let layer = ServiceBuilder::new() .map_err(Into::into) .layer(MapResponseLayer::new(IntoResponse::into_response))