Skip to content

Commit

Permalink
Annotate panicking functions with #[track_caller] (#1248)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidpdrsn committed Oct 20, 2022
1 parent e5f7012 commit 29fea3a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
6 changes: 5 additions & 1 deletion axum/CHANGELOG.md
Expand Up @@ -7,7 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

# Unreleased

- None.
- **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])

[#1248]: https://github.com/tokio-rs/axum/pull/1248

# 0.5.16 (10. September, 2022)

Expand Down
9 changes: 9 additions & 0 deletions axum/src/routing/method_routing.rs
Expand Up @@ -207,6 +207,7 @@ macro_rules! chained_service_fn {
$name:ident, $method:ident
) => {
$(#[$m])+
#[track_caller]
pub fn $name<S, ResBody>(self, svc: S) -> Self
where
S: Service<Request<ReqBody>, Response = Response<ResBody>, Error = E>
Expand Down Expand Up @@ -271,6 +272,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 @@ -585,6 +587,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 @@ -697,6 +700,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, ResBody>(self, filter: MethodFilter, svc: S) -> Self
where
S: Service<Request<ReqBody>, Response = Response<ResBody>, Error = E>
Expand Down Expand Up @@ -809,8 +813,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 @@ -822,6 +828,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 @@ -868,12 +875,14 @@ 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>, Response = Response, Error = E> + Clone + Send + 'static,
S::Future: Send + 'static,
{
// written using an inner function to generate less IR
#[track_caller]
fn set_service<T>(
method_name: &str,
out: &mut Option<T>,
Expand Down
5 changes: 5 additions & 0 deletions axum/src/routing/mod.rs
Expand Up @@ -121,6 +121,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>, Response = Response, Error = Infallible> + Clone + Send + 'static,
Expand Down Expand Up @@ -173,6 +174,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 @@ -183,6 +185,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>, Response = Response, Error = Infallible> + Clone + Send + 'static,
Expand Down Expand Up @@ -262,6 +265,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 Expand Up @@ -477,6 +481,7 @@ where
}
}

#[track_caller]
fn panic_on_matchit_error(&self, err: matchit::InsertError) {
if self.nested_at_root {
panic!(
Expand Down

0 comments on commit 29fea3a

Please sign in to comment.