Skip to content

Commit

Permalink
fix: Include RouteAliases in RouteNotFoundError message (#13908) (#13920
Browse files Browse the repository at this point in the history
)

When navigating to a non-existent route in dev mode, an error page is displayed with the list of the available routes. However, as noted by @dmitrilc, this list doesn't include routes defined via the @RouteAlias annotation.

Co-authored-by: Tarek Oraby <[email protected]>
  • Loading branch information
vaadin-bot and tarekoraby authored Jun 8, 2022
1 parent e61f5df commit d426a9c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;

import org.apache.commons.io.IOUtils;
Expand Down Expand Up @@ -107,25 +110,34 @@ private static String readHtmlFile(String templateName) {
private String getRoutes(BeforeEnterEvent event) {
List<RouteData> routes = event.getSource().getRegistry()
.getRegisteredRoutes();
Map<String, Class<? extends Component>> routeTemplates = new TreeMap<>();

return routes.stream()
.sorted((route1, route2) -> route1.getTemplate()
.compareTo(route2.getTemplate()))
.map(this::routeToHtml).map(Element::outerHtml)
for (RouteData route : routes) {
routeTemplates.put(route.getTemplate(),
route.getNavigationTarget());
route.getRouteAliases().forEach(alias -> routeTemplates
.put(alias.getTemplate(), alias.getNavigationTarget()));
}

List<Element> routeElements = new ArrayList<>();
routeTemplates.forEach(
(k, v) -> routeElements.add(routeTemplateToHtml(k, v)));

return routeElements.stream().map(Element::outerHtml)
.collect(Collectors.joining());
}

private Element routeToHtml(RouteData route) {
String text = route.getTemplate();
private Element routeTemplateToHtml(String routeTemplate,
Class<? extends Component> navigationTarget) {
String text = routeTemplate;
if (text == null || text.isEmpty()) {
text = "<root>";
}

if (!route.getTemplate().contains(":")) {
return elementAsLink(route.getTemplate(), text);
if (!routeTemplate.contains(":")) {
return elementAsLink(routeTemplate, text);
} else {
Class<? extends Component> target = route.getNavigationTarget();
if (ParameterDeserializer.isAnnotatedParameter(target,
if (ParameterDeserializer.isAnnotatedParameter(navigationTarget,
OptionalParameter.class)) {
text += " (supports optional parameter)";
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ public void faultyRouteShowsExpectedErrorScreen() {

Assert.assertTrue(getDriver().getPageSource()
.contains("Could not navigate to 'exception'"));

Assert.assertTrue(getDriver().getPageSource()
.contains(RouterTestServlet.AliasLayout.class
.getAnnotation(Route.class).value()));

Assert.assertTrue(getDriver().getPageSource()
.contains(RouterTestServlet.AliasLayout.class
.getAnnotation(RouteAlias.class).value()));
}

@Test
Expand Down

0 comments on commit d426a9c

Please sign in to comment.