Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Annotate panicking functions with #[track_caller] #1248

Merged
merged 2 commits into from Aug 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion axum/CHANGELOG.md
Expand Up @@ -42,8 +42,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
relaxed so the response type must implement `IntoResponse` rather than being a
literal `Response`
- **change:** axum's MSRV is now 1.60 ([#1239])
- **fixed:** Annotate panicking functions with `#[track_caller]` so the error
message points to where the user added the invalid router, rather than
somewhere internally in axum ([#1248])

[#1171]: https://github.com/tokio-rs/axum/pull/1171
[#1077]: https://github.com/tokio-rs/axum/pull/1077
[#1086]: https://github.com/tokio-rs/axum/pull/1086
[#1088]: https://github.com/tokio-rs/axum/pull/1088
Expand All @@ -52,7 +54,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[#1130]: https://github.com/tokio-rs/axum/pull/1130
[#1135]: https://github.com/tokio-rs/axum/pull/1135
[#1152]: https://github.com/tokio-rs/axum/pull/1152
[#1171]: https://github.com/tokio-rs/axum/pull/1171
[#1239]: https://github.com/tokio-rs/axum/pull/1239
[#1248]: https://github.com/tokio-rs/axum/pull/1248
[#924]: https://github.com/tokio-rs/axum/pull/924

# 0.5.10 (28. June, 2022)
Expand Down
8 changes: 8 additions & 0 deletions axum/src/routing/method_routing.rs
Expand Up @@ -205,6 +205,7 @@ macro_rules! chained_service_fn {
$name:ident, $method:ident
) => {
$(#[$m])+
#[track_caller]
pub fn $name<S>(self, svc: S) -> Self
where
S: Service<Request<ReqBody>, Error = E>
Expand Down Expand Up @@ -268,6 +269,7 @@ macro_rules! chained_handler_fn {
$name:ident, $method:ident
) => {
$(#[$m])+
#[track_caller]
pub fn $name<H, T>(self, handler: H) -> Self
where
H: Handler<T, B>,
Expand Down Expand Up @@ -577,6 +579,7 @@ where
/// # axum::Server::bind(&"".parse().unwrap()).serve(app.into_make_service()).await.unwrap();
/// # };
/// ```
#[track_caller]
pub fn on<H, T>(self, filter: MethodFilter, handler: H) -> Self
where
H: Handler<T, B>,
Expand Down Expand Up @@ -689,6 +692,7 @@ impl<ReqBody, E> MethodRouter<ReqBody, E> {
/// # axum::Server::bind(&"".parse().unwrap()).serve(app.into_make_service()).await.unwrap();
/// # };
/// ```
#[track_caller]
pub fn on_service<S>(self, filter: MethodFilter, svc: S) -> Self
where
S: Service<Request<ReqBody>, Error = E> + Clone + Send + 'static,
Expand Down Expand Up @@ -783,8 +787,10 @@ impl<ReqBody, E> MethodRouter<ReqBody, E> {
}

#[doc = include_str!("../docs/method_routing/merge.md")]
#[track_caller]
pub fn merge(mut self, other: MethodRouter<ReqBody, E>) -> Self {
// written using inner functions to generate less IR
#[track_caller]
fn merge_inner<T>(name: &str, first: Option<T>, second: Option<T>) -> Option<T> {
match (first, second) {
(Some(_), Some(_)) => panic!(
Expand All @@ -796,6 +802,7 @@ impl<ReqBody, E> MethodRouter<ReqBody, E> {
}
}

#[track_caller]
fn merge_fallback<B, E>(
fallback: Fallback<B, E>,
fallback_other: Fallback<B, E>,
Expand Down Expand Up @@ -843,6 +850,7 @@ impl<ReqBody, E> MethodRouter<ReqBody, E> {
self.layer(HandleErrorLayer::new(f))
}

#[track_caller]
fn on_service_boxed_response_body<S>(mut self, filter: MethodFilter, svc: S) -> Self
where
S: Service<Request<ReqBody>, Error = E> + Clone + Send + 'static,
Expand Down
4 changes: 4 additions & 0 deletions axum/src/routing/mod.rs
Expand Up @@ -114,6 +114,7 @@ where
}

#[doc = include_str!("../docs/routing/route.md")]
#[track_caller]
pub fn route<T>(mut self, path: &str, service: T) -> Self
where
T: Service<Request<B>, Error = Infallible> + Clone + Send + 'static,
Expand Down Expand Up @@ -167,6 +168,7 @@ where
self
}

#[track_caller]
fn set_node(&mut self, path: &str, id: RouteId) {
let mut node =
Arc::try_unwrap(Arc::clone(&self.node)).unwrap_or_else(|node| (*node).clone());
Expand All @@ -177,6 +179,7 @@ where
}

#[doc = include_str!("../docs/routing/nest.md")]
#[track_caller]
pub fn nest<T>(mut self, mut path: &str, svc: T) -> Self
where
T: Service<Request<B>, Error = Infallible> + Clone + Send + 'static,
Expand Down Expand Up @@ -216,6 +219,7 @@ where
}

#[doc = include_str!("../docs/routing/merge.md")]
#[track_caller]
pub fn merge<R>(mut self, other: R) -> Self
where
R: Into<Router<B>>,
Expand Down