mirror of
https://github.com/tokio-rs/axum.git
synced 2026-08-29 00:00:18 +02:00
Panic instead of silently discarding fallbacks (#529)
This introduces two new possible panics when constructing routers: - If merging two routers that each have a fallback. Previously that left side fallback would be silently discarded. - If nesting a router that has a fallback. Previously it would be silently discarded. Overall this should make things more explicit and users shouldn't have to worry "why isn't my fallback" working. Fixes #488
This commit is contained in:
@@ -189,14 +189,17 @@ where
|
||||
let Router {
|
||||
mut routes,
|
||||
node,
|
||||
// discard the fallback of the nested router
|
||||
fallback: _,
|
||||
fallback,
|
||||
// nesting a router that has something nested at root
|
||||
// doesn't mean something is nested at root in _this_ router
|
||||
// thus we don't need to propagate that
|
||||
nested_at_root: _,
|
||||
} = router;
|
||||
|
||||
if let Fallback::Custom(_) = fallback {
|
||||
panic!("Cannot nest `Router`s that has a fallback");
|
||||
}
|
||||
|
||||
for (id, nested_path) in node.route_id_to_path {
|
||||
let route = routes.remove(&id).unwrap();
|
||||
let full_path = if &*nested_path == "/" {
|
||||
@@ -253,7 +256,9 @@ where
|
||||
(Fallback::Default(_), pick @ Fallback::Default(_)) => pick,
|
||||
(Fallback::Default(_), pick @ Fallback::Custom(_)) => pick,
|
||||
(pick @ Fallback::Custom(_), Fallback::Default(_)) => pick,
|
||||
(Fallback::Custom(_), pick @ Fallback::Custom(_)) => pick,
|
||||
(Fallback::Custom(_), Fallback::Custom(_)) => {
|
||||
panic!("Cannot merge two `Router`s that both have a fallback")
|
||||
}
|
||||
};
|
||||
|
||||
self.nested_at_root = self.nested_at_root || nested_at_root;
|
||||
|
||||
Reference in New Issue
Block a user