Skip to content

Commit 049bfdf

Browse files
authored
fix: make route config errors more prominent (#20847)
When route configuration errors happen, make sure to output the actual error message as the first output at ERROR level. When this is detected at startup, we can't prevent printing out the stack trace too since the container does that when initialization throws. Also checked all the messages for different route misconfiguration cases, and they seem to be fine. Fixes #20589
1 parent 8dd3a20 commit 049bfdf

9 files changed

Lines changed: 87 additions & 62 deletions

File tree

‎flow-server/src/main/java/com/vaadin/flow/router/internal/RouteSegment.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -791,9 +791,9 @@ private RuntimeException ambigousTarget(Class<? extends Component> target) {
791791

792792
String messageFormat;
793793
if (isParameter()) {
794-
messageFormat = "Navigation targets must have unique routes, found navigation targets '%s' and '%s' with parameter have the same route.";
794+
messageFormat = RouteUtil.ROUTE_CONFLICT_WITH_PARAMS;
795795
} else {
796-
messageFormat = "Navigation targets must have unique routes, found navigation targets '%s' and '%s' with the same route.";
796+
messageFormat = RouteUtil.ROUTE_CONFLICT;
797797
}
798798

799799
String message = String.format(messageFormat,

‎flow-server/src/main/java/com/vaadin/flow/router/internal/RouteUtil.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@
7070
*/
7171
public class RouteUtil {
7272

73+
private static final String ROUTE_CONFLICT_PREFIX = "Navigation target paths (considering @Route, @RouteAlias and @RoutePrefix values) must be unique, found navigation targets '%s' and '%s' ";
74+
public static final String ROUTE_CONFLICT = ROUTE_CONFLICT_PREFIX
75+
+ "having the same route.";
76+
public static final String ROUTE_CONFLICT_WITH_PARAMS = ROUTE_CONFLICT_PREFIX
77+
+ "with parameter having the same route.";
78+
7379
protected RouteUtil() {
7480
}
7581

‎flow-server/src/main/java/com/vaadin/flow/server/DefaultErrorHandler.java‎

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,33 +19,15 @@
1919
import java.io.EOFException;
2020
import java.net.SocketException;
2121
import java.net.SocketTimeoutException;
22-
import java.util.ArrayList;
23-
import java.util.List;
24-
import java.util.Optional;
22+
import java.util.HashSet;
2523
import java.util.Set;
26-
import java.util.stream.Collectors;
2724

2825
import org.slf4j.Logger;
2926
import org.slf4j.LoggerFactory;
3027
import org.slf4j.Marker;
3128
import org.slf4j.MarkerFactory;
3229

33-
import com.vaadin.experimental.FeatureFlags;
34-
import com.vaadin.flow.component.Component;
35-
import com.vaadin.flow.component.HasElement;
36-
import com.vaadin.flow.component.UI;
37-
import com.vaadin.flow.di.Instantiator;
38-
import com.vaadin.flow.router.BeforeEnterEvent;
39-
import com.vaadin.flow.router.ErrorParameter;
40-
import com.vaadin.flow.router.HasErrorParameter;
4130
import com.vaadin.flow.router.InvalidLocationException;
42-
import com.vaadin.flow.router.NavigationEvent;
43-
import com.vaadin.flow.router.NavigationTrigger;
44-
import com.vaadin.flow.router.RouteConfiguration;
45-
import com.vaadin.flow.router.RouterLayout;
46-
import com.vaadin.flow.router.internal.ErrorTargetEntry;
47-
import com.vaadin.flow.router.internal.RouteUtil;
48-
import com.vaadin.flow.server.startup.ApplicationRouteRegistry;
4931

5032
/**
5133
* The default implementation of {@link ErrorHandler}.
@@ -74,9 +56,18 @@
7456
public class DefaultErrorHandler implements ErrorHandler {
7557

7658
private final Set<String> ignoredExceptions;
59+
private final Set<String> routeConfigurationExceptions;
7760

7861
protected DefaultErrorHandler(Set<String> ignoredExceptions) {
7962
this.ignoredExceptions = Set.copyOf(ignoredExceptions);
63+
this.routeConfigurationExceptions = new HashSet<>();
64+
}
65+
66+
protected DefaultErrorHandler(Set<String> ignoredExceptions,
67+
Set<String> routeConfigurationExceptions) {
68+
this.ignoredExceptions = Set.copyOf(ignoredExceptions);
69+
this.routeConfigurationExceptions = Set
70+
.copyOf(routeConfigurationExceptions);
8071
}
8172

8273
public DefaultErrorHandler() {
@@ -85,6 +76,10 @@ public DefaultErrorHandler() {
8576
EOFException.class.getName(),
8677
"org.eclipse.jetty.io.EofException",
8778
"org.apache.catalina.connector.ClientAbortException");
79+
this.routeConfigurationExceptions = Set.of(
80+
AmbiguousRouteConfigurationException.class.getName(),
81+
InvalidRouteConfigurationException.class.getName(),
82+
InvalidRouteLayoutConfigurationException.class.getName());
8883
}
8984

9085
@Override
@@ -101,8 +96,14 @@ public void error(ErrorEvent event) {
10196
getLogger().warn(marker, "", throwable);
10297
}
10398
} else {
104-
// print the error on console
105-
getLogger().error("", throwable);
99+
if (routeConfigurationExceptions
100+
.contains(throwable.getClass().getName())) {
101+
getLogger().error("Route configuration error found:");
102+
getLogger().error(throwable.getMessage());
103+
} else {
104+
// print the error on console
105+
getLogger().error("", throwable);
106+
}
106107
}
107108
}
108109
}

‎flow-server/src/main/java/com/vaadin/flow/server/startup/VaadinServletContextStartupInitializer.java‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,17 @@
1515
*/
1616
package com.vaadin.flow.server.startup;
1717

18+
import com.vaadin.flow.server.InvalidRouteConfigurationException;
19+
import com.vaadin.flow.server.InvalidRouteLayoutConfigurationException;
1820
import com.vaadin.flow.server.VaadinServletContext;
1921

2022
import jakarta.servlet.ServletContext;
2123
import jakarta.servlet.ServletException;
2224
import java.util.Set;
2325

26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
28+
2429
/**
2530
* Allows a library/runtime to be notified of a web application's startup phase
2631
* and perform any required programmatic registration of servlets, filters, and
@@ -41,8 +46,17 @@ default void process(Set<Class<?>> classSet, ServletContext context)
4146
try {
4247
initialize(classSet, new VaadinServletContext(context));
4348
} catch (VaadinInitializerException e) {
49+
if (e.getCause() instanceof InvalidRouteConfigurationException || e
50+
.getCause() instanceof InvalidRouteLayoutConfigurationException) {
51+
getLogger().error("Route configuration error found:");
52+
getLogger().error(e.getCause().getMessage());
53+
}
4454
throw new ServletException(e);
4555
}
4656
}
4757

58+
private static Logger getLogger() {
59+
return LoggerFactory
60+
.getLogger(VaadinServletContextStartupInitializer.class);
61+
}
4862
}

‎flow-server/src/test/java/com/vaadin/flow/router/internal/AbstractRouteRegistryTest.java‎

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,27 @@
66
import java.util.List;
77
import java.util.concurrent.CountDownLatch;
88

9-
import com.vaadin.flow.router.BeforeEvent;
10-
import com.vaadin.flow.router.HasUrlParameter;
11-
import com.vaadin.flow.router.NotFoundException;
12-
import com.vaadin.flow.router.OptionalParameter;
13-
import com.vaadin.flow.router.RouteParameterRegex;
14-
import com.vaadin.flow.router.WildcardParameter;
15-
import com.vaadin.flow.server.InvalidRouteConfigurationException;
169
import org.junit.Assert;
1710
import org.junit.Before;
1811
import org.junit.Rule;
1912
import org.junit.Test;
13+
import org.junit.rules.ExpectedException;
2014

2115
import com.vaadin.flow.component.Component;
2216
import com.vaadin.flow.component.Tag;
17+
import com.vaadin.flow.router.BeforeEvent;
18+
import com.vaadin.flow.router.HasUrlParameter;
19+
import com.vaadin.flow.router.Layout;
20+
import com.vaadin.flow.router.NotFoundException;
21+
import com.vaadin.flow.router.OptionalParameter;
2322
import com.vaadin.flow.router.Route;
2423
import com.vaadin.flow.router.RouteBaseData;
24+
import com.vaadin.flow.router.RouteParameterRegex;
2525
import com.vaadin.flow.router.RouterLayout;
2626
import com.vaadin.flow.router.RoutesChangedEvent;
27-
import com.vaadin.flow.router.Layout;
27+
import com.vaadin.flow.router.WildcardParameter;
28+
import com.vaadin.flow.server.InvalidRouteConfigurationException;
2829
import com.vaadin.flow.shared.Registration;
29-
import org.junit.rules.ExpectedException;
3030

3131
public class AbstractRouteRegistryTest {
3232

@@ -620,8 +620,7 @@ private void assertHasUrlAndWildcard() {
620620
public void multiple_normal_routes_throw_exception()
621621
throws InvalidRouteConfigurationException {
622622
expectedEx.expect(InvalidRouteConfigurationException.class);
623-
expectedEx.expectMessage(String.format(
624-
"Navigation targets must have unique routes, found navigation targets '%s' and '%s' with the same route.",
623+
expectedEx.expectMessage(String.format(RouteUtil.ROUTE_CONFLICT,
625624
NormalRoute.class.getName(),
626625
SecondNormalRoute.class.getName()));
627626

@@ -648,10 +647,10 @@ public void normal_and_optional_throws_exception()
648647
public void two_optionals_throw_exception()
649648
throws InvalidRouteConfigurationException {
650649
expectedEx.expect(InvalidRouteConfigurationException.class);
651-
expectedEx.expectMessage(String.format(
652-
"Navigation targets must have unique routes, found navigation targets '%s' and '%s' with parameter have the same route.",
653-
OptionalRoute.class.getName(),
654-
SecondOptionalRoute.class.getName()));
650+
expectedEx.expectMessage(
651+
String.format(RouteUtil.ROUTE_CONFLICT_WITH_PARAMS,
652+
OptionalRoute.class.getName(),
653+
SecondOptionalRoute.class.getName()));
655654

656655
addTarget(OptionalRoute.class);
657656
addTarget(SecondOptionalRoute.class);
@@ -675,10 +674,10 @@ public void optional_and_normal_throws_exception()
675674
public void two_has_route_parameters_throw_exception()
676675
throws InvalidRouteConfigurationException {
677676
expectedEx.expect(InvalidRouteConfigurationException.class);
678-
expectedEx.expectMessage(String.format(
679-
"Navigation targets must have unique routes, found navigation targets '%s' and '%s' with parameter have the same route.",
680-
HasUrlRoute.class.getName(),
681-
SecondHasUrlRoute.class.getName()));
677+
expectedEx.expectMessage(
678+
String.format(RouteUtil.ROUTE_CONFLICT_WITH_PARAMS,
679+
HasUrlRoute.class.getName(),
680+
SecondHasUrlRoute.class.getName()));
682681

683682
addTarget(HasUrlRoute.class);
684683
addTarget(SecondHasUrlRoute.class);
@@ -689,10 +688,10 @@ public void two_has_route_parameters_throw_exception()
689688
public void two_wildcard_parameters_throw_exception()
690689
throws InvalidRouteConfigurationException {
691690
expectedEx.expect(InvalidRouteConfigurationException.class);
692-
expectedEx.expectMessage(String.format(
693-
"Navigation targets must have unique routes, found navigation targets '%s' and '%s' with parameter have the same route.",
694-
WildcardRoute.class.getName(),
695-
SecondWildcardRoute.class.getName()));
691+
expectedEx.expectMessage(
692+
String.format(RouteUtil.ROUTE_CONFLICT_WITH_PARAMS,
693+
WildcardRoute.class.getName(),
694+
SecondWildcardRoute.class.getName()));
696695

697696
addTarget(WildcardRoute.class);
698697
addTarget(SecondWildcardRoute.class);

‎flow-server/src/test/java/com/vaadin/flow/router/internal/ConfigureRoutesTest.java‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package com.vaadin.flow.router.internal;
22

3+
import org.junit.Assert;
4+
import org.junit.Rule;
5+
import org.junit.Test;
6+
import org.junit.rules.ExpectedException;
7+
38
import com.vaadin.flow.component.Component;
49
import com.vaadin.flow.component.Tag;
510
import com.vaadin.flow.router.BeforeEnterEvent;
@@ -8,10 +13,6 @@
813
import com.vaadin.flow.router.HasErrorParameter;
914
import com.vaadin.flow.router.HasUrlParameter;
1015
import com.vaadin.flow.server.AmbiguousRouteConfigurationException;
11-
import org.junit.Assert;
12-
import org.junit.Rule;
13-
import org.junit.Test;
14-
import org.junit.rules.ExpectedException;
1516

1617
public class ConfigureRoutesTest {
1718

@@ -109,8 +110,9 @@ public void duplicateRootPathRegistration_throwsException() {
109110
exceptionRule.expect(AmbiguousRouteConfigurationException.class);
110111
exceptionRule.reportMissingExceptionWithMessage(
111112
"Duplicate routes shouldn't be accepted.");
112-
exceptionRule.expectMessage(
113-
"Navigation targets must have unique routes, found navigation targets 'com.vaadin.flow.router.internal.ConfigureRoutesTest$BaseTarget' and 'com.vaadin.flow.router.internal.ConfigureRoutesTest$BaseTarget' with the same route.");
113+
exceptionRule.expectMessage(String.format(RouteUtil.ROUTE_CONFLICT,
114+
"com.vaadin.flow.router.internal.ConfigureRoutesTest$BaseTarget",
115+
"com.vaadin.flow.router.internal.ConfigureRoutesTest$BaseTarget"));
114116
mutable.setRoute("", BaseTarget.class);
115117
}
116118

@@ -126,8 +128,10 @@ public void duplicateParameterPathRegistration_throwsException() {
126128
exceptionRule.expect(AmbiguousRouteConfigurationException.class);
127129
exceptionRule.reportMissingExceptionWithMessage(
128130
"Duplicate parameter routes shouldn't be accepted.");
129-
exceptionRule.expectMessage(
130-
"Navigation targets must have unique routes, found navigation targets 'com.vaadin.flow.router.internal.ConfigureRoutesTest$ParamTarget' and 'com.vaadin.flow.router.internal.ConfigureRoutesTest$ParamTarget' with parameter have the same route.");
131+
exceptionRule.expectMessage(String.format(
132+
RouteUtil.ROUTE_CONFLICT_WITH_PARAMS,
133+
"com.vaadin.flow.router.internal.ConfigureRoutesTest$ParamTarget",
134+
"com.vaadin.flow.router.internal.ConfigureRoutesTest$ParamTarget"));
131135
mutable.setRoute(":param", ParamTarget.class);
132136
}
133137

‎flow-server/src/test/java/com/vaadin/flow/server/SessionRouteRegistryTest.java‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import com.vaadin.flow.router.RouterLayout;
5050
import com.vaadin.flow.router.RoutesChangedEvent;
5151
import com.vaadin.flow.router.internal.HasUrlParameterFormat;
52+
import com.vaadin.flow.router.internal.RouteUtil;
5253
import com.vaadin.flow.server.startup.ApplicationConfiguration;
5354
import com.vaadin.flow.server.startup.ApplicationRouteRegistry;
5455
import com.vaadin.flow.shared.Registration;
@@ -488,8 +489,7 @@ public void setSameRouteValueFromDifferentThreads_ConcurrencyTest()
488489
Assert.assertEquals(
489490
"Expected 4 route already exists exceptions due to route target validation",
490491
THREADS - 1, exceptions.size());
491-
String expected = String.format(
492-
"Navigation targets must have unique routes, found navigation targets '%s' and '%s' with the same route.",
492+
String expected = String.format(RouteUtil.ROUTE_CONFLICT,
493493
MyRoute.class.getName(), MyRoute.class.getName());
494494
for (String exception : exceptions) {
495495
Assert.assertEquals(expected, exception);
@@ -540,8 +540,7 @@ public void useRouteResolutionFromDifferentThreads_ConcurrencyTest()
540540
Assert.assertEquals(
541541
"Expected 4 route already exists exceptions due to route target validation",
542542
THREADS - 1, exceptions.size());
543-
String expected = String.format(
544-
"Navigation targets must have unique routes, found navigation targets '%s' and '%s' with the same route.",
543+
String expected = String.format(RouteUtil.ROUTE_CONFLICT,
545544
MyRoute.class.getName(), MyRoute.class.getName());
546545
for (String exception : exceptions) {
547546
Assert.assertEquals(expected, exception);

‎flow-server/src/test/java/com/vaadin/flow/server/startup/RouteRegistryInitializerTest.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
import com.vaadin.flow.router.internal.ErrorTargetEntry;
7070
import com.vaadin.flow.router.internal.HasUrlParameterFormat;
7171
import com.vaadin.flow.router.internal.PathUtil;
72+
import com.vaadin.flow.router.internal.RouteUtil;
7273
import com.vaadin.flow.server.InvalidRouteConfigurationException;
7374
import com.vaadin.flow.server.InvalidRouteLayoutConfigurationException;
7475
import com.vaadin.flow.server.MockVaadinContext;
@@ -169,10 +170,9 @@ public void process_duplicate_routesViaAlias_throws()
169170
@Test
170171
public void routeRegistry_fails_for_multiple_registration_of_same_route() {
171172
expectedEx.expect(InvalidRouteConfigurationException.class);
172-
expectedEx.expectMessage(
173-
"Navigation targets must have unique routes, found navigation targets "
174-
+ "'com.vaadin.flow.server.startup.RouteRegistryInitializerTest$NavigationTargetFoo' and "
175-
+ "'com.vaadin.flow.server.startup.RouteRegistryInitializerTest$NavigationTargetFoo2' with the same route.");
173+
expectedEx.expectMessage(String.format(RouteUtil.ROUTE_CONFLICT,
174+
"com.vaadin.flow.server.startup.RouteRegistryInitializerTest$NavigationTargetFoo",
175+
"com.vaadin.flow.server.startup.RouteRegistryInitializerTest$NavigationTargetFoo2"));
176176

177177
RouteConfiguration.forRegistry(registry)
178178
.setAnnotatedRoute(NavigationTargetFoo.class);

‎vaadin-spring/src/main/java/com/vaadin/flow/spring/VaadinServletContextInitializer.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,8 @@ public void failFastContextInitialized(ServletContextEvent event) {
393393

394394
} catch (InvalidRouteConfigurationException
395395
| InvalidRouteLayoutConfigurationException e) {
396+
getLogger().error("Route configuration error found:");
397+
getLogger().error(e.getMessage());
396398
throw new IllegalStateException(e);
397399
}
398400
} else {

0 commit comments

Comments
 (0)