Skip to content

Commit 43b5efc

Browse files
authored
fix: Annotate default error handlers. (#11063)
Annotate default error handlers with the new annotaton DefaultErrorHandler so that we can also in add-ons have default handlers that can be overridden by user handlers. Part of vaadin/spring#661
1 parent b8c355f commit 43b5efc

6 files changed

Lines changed: 104 additions & 27 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.vaadin.flow.component.Tag;
3131
import com.vaadin.flow.dom.Element;
3232
import com.vaadin.flow.dom.ElementFactory;
33+
import com.vaadin.flow.router.internal.DefaultErrorHandler;
3334
import com.vaadin.flow.server.VaadinService;
3435
import com.vaadin.flow.server.auth.AnonymousAllowed;
3536

@@ -40,6 +41,7 @@
4041
*/
4142
@Tag(Tag.DIV)
4243
@AnonymousAllowed
44+
@DefaultErrorHandler
4345
public class InternalServerError extends Component
4446
implements HasErrorParameter<Exception> {
4547

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.vaadin.flow.component.Component;
3232
import com.vaadin.flow.component.Html;
3333
import com.vaadin.flow.component.Tag;
34+
import com.vaadin.flow.router.internal.DefaultErrorHandler;
3435
import com.vaadin.flow.server.auth.AnonymousAllowed;
3536

3637
/**
@@ -40,6 +41,7 @@
4041
*/
4142
@Tag(Tag.DIV)
4243
@AnonymousAllowed
44+
@DefaultErrorHandler
4345
public class RouteNotFoundError extends Component
4446
implements HasErrorParameter<NotFoundException> {
4547

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import com.vaadin.flow.router.RoutesChangedListener;
3939
import com.vaadin.flow.server.Command;
4040
import com.vaadin.flow.server.InvalidRouteConfigurationException;
41-
import com.vaadin.flow.server.InvalidRouteLayoutConfigurationException;
4241
import com.vaadin.flow.server.RouteRegistry;
4342
import com.vaadin.flow.shared.Registration;
4443

@@ -413,14 +412,19 @@ protected void addErrorTarget(Class<? extends Component> target,
413412
* Register a child handler if parent registered or leave as is if child
414413
* registered.
415414
* <p>
416-
* If the target is not related to the registered handler then throw
415+
* If the target is not related to the registered handler and neither
416+
* handler is annotated as {@link DefaultErrorHandler} then throw
417417
* configuration exception as only one handler for each exception type is
418418
* allowed.
419419
*
420420
* @param target
421421
* target being handled
422422
* @param exceptionType
423423
* type of the handled exception
424+
* @throws InvalidRouteConfigurationException
425+
* thrown if multiple exception handlers are registered for the
426+
* same exception without relation or the other being a default
427+
* handler
424428
*/
425429
private void handleRegisteredExceptionType(
426430
Map<Class<? extends Exception>, Class<? extends Component>> exceptionTargetsMap,
@@ -432,11 +436,15 @@ private void handleRegisteredExceptionType(
432436
if (registered.isAssignableFrom(target)) {
433437
exceptionTargetsMap.put(exceptionType, target);
434438
} else if (!target.isAssignableFrom(registered)) {
435-
String msg = String.format(
436-
"Only one target for an exception should be defined. Found '%s' and '%s' for exception '%s'",
437-
target.getName(), registered.getName(),
438-
exceptionType.getName());
439-
throw new InvalidRouteLayoutConfigurationException(msg);
439+
if (registered.isAnnotationPresent(DefaultErrorHandler.class)) {
440+
exceptionTargetsMap.put(exceptionType, target);
441+
} else if (!target.isAnnotationPresent(DefaultErrorHandler.class)) {
442+
String msg = String.format(
443+
"Only one target for an exception should be defined. Found '%s' and '%s' for exception '%s'",
444+
target.getName(), registered.getName(),
445+
exceptionType.getName());
446+
throw new InvalidRouteConfigurationException(msg);
447+
}
440448
}
441449
}
442450

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2000-2021 Vaadin Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.vaadin.flow.router.internal;
18+
19+
import java.lang.annotation.Documented;
20+
import java.lang.annotation.ElementType;
21+
import java.lang.annotation.Retention;
22+
import java.lang.annotation.RetentionPolicy;
23+
import java.lang.annotation.Target;
24+
25+
/**
26+
* Marks an HasErrorParameter view as Framework default handler so it can be
27+
* disregarded if there is a custom view for the same Exception.
28+
*
29+
* @since
30+
*/
31+
@Retention(RetentionPolicy.RUNTIME)
32+
@Target(ElementType.TYPE)
33+
@Documented
34+
public @interface DefaultErrorHandler {
35+
}

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

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,20 @@
2121
import java.util.HashMap;
2222
import java.util.List;
2323
import java.util.Map;
24-
import java.util.Objects;
2524
import java.util.Optional;
2625
import java.util.ServiceLoader;
2726
import java.util.Set;
2827
import java.util.concurrent.atomic.AtomicReference;
29-
import java.util.stream.Collectors;
30-
import java.util.stream.Stream;
3128

3229
import org.slf4j.LoggerFactory;
3330

3431
import com.vaadin.flow.component.Component;
3532
import com.vaadin.flow.router.InternalServerError;
3633
import com.vaadin.flow.router.NotFoundException;
3734
import com.vaadin.flow.router.RouteNotFoundError;
38-
import com.vaadin.flow.router.RouteParameters;
3935
import com.vaadin.flow.router.RouterLayout;
4036
import com.vaadin.flow.router.internal.AbstractRouteRegistry;
4137
import com.vaadin.flow.router.internal.ErrorTargetEntry;
42-
import com.vaadin.flow.router.internal.NavigationRouteTarget;
43-
import com.vaadin.flow.router.internal.PathUtil;
44-
import com.vaadin.flow.router.internal.RouteTarget;
4538
import com.vaadin.flow.server.ErrorRouteRegistry;
4639
import com.vaadin.flow.server.PWA;
4740
import com.vaadin.flow.server.VaadinContext;
@@ -56,9 +49,6 @@ public class ApplicationRouteRegistry extends AbstractRouteRegistry
5649
implements ErrorRouteRegistry {
5750

5851
private AtomicReference<Class<?>> pwaConfigurationClass = new AtomicReference<>();
59-
private static final Set<Class<? extends Component>> defaultErrorHandlers = Stream
60-
.of(RouteNotFoundError.class, InternalServerError.class)
61-
.collect(Collectors.toSet());
6252

6353
private final ArrayList<NavigationTargetFilter> routeFilters = new ArrayList<>();
6454

@@ -156,9 +146,7 @@ public void setErrorNavigationTargets(
156146

157147
exceptionTargetsMap.putAll(getConfiguration().getExceptionHandlers());
158148

159-
errorNavigationTargets.stream()
160-
.filter(target -> !defaultErrorHandlers.contains(target))
161-
.filter(this::allErrorFiltersMatch)
149+
errorNavigationTargets.stream().filter(this::allErrorFiltersMatch)
162150
.filter(handler -> !Modifier.isAbstract(handler.getModifiers()))
163151
.forEach(target -> addErrorTarget(target, exceptionTargetsMap));
164152

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

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import com.vaadin.flow.router.BeforeLeaveEvent.ContinueNavigationAction;
4747
import com.vaadin.flow.router.RouterTest.CombinedObserverTarget.Enter;
4848
import com.vaadin.flow.router.RouterTest.CombinedObserverTarget.Leave;
49+
import com.vaadin.flow.router.internal.DefaultErrorHandler;
4950
import com.vaadin.flow.router.internal.HasUrlParameterFormat;
5051
import com.vaadin.flow.router.internal.RouteUtil;
5152
import com.vaadin.flow.server.InvalidRouteConfigurationException;
@@ -2327,24 +2328,65 @@ public void exception_during_navigation_is_caught_and_show_in_internalServerErro
23272328
}
23282329

23292330
@Test
2330-
public void fail_for_multiple_of_the_same_class()
2331+
public void fail_for_multiple_classes_extending_the_same_exception_class()
23312332
throws InvalidRouteConfigurationException {
2332-
setErrorNavigationTargets(ErrorTarget.class, RouteNotFoundError.class);
2333+
expectedEx.expect(InvalidRouteConfigurationException.class);
2334+
setErrorNavigationTargets(ErrorTarget.class,
2335+
CustomNotFoundTarget.class);
2336+
}
23332337

2334-
int result = router.navigate(ui, new Location("exception"),
2338+
@Route("npe")
2339+
@Tag(Tag.DIV)
2340+
public static class NpeNavigationTarget extends Component {
2341+
public NpeNavigationTarget() {
2342+
throw new NullPointerException("Null was found");
2343+
}
2344+
}
2345+
2346+
@Tag(Tag.DIV)
2347+
@DefaultErrorHandler
2348+
public static class DefaultNullPointerException extends Component
2349+
implements HasErrorParameter<NullPointerException> {
2350+
2351+
@Override
2352+
public int setErrorParameter(BeforeEnterEvent event,
2353+
ErrorParameter<NullPointerException> parameter) {
2354+
return HttpServletResponse.SC_UNAUTHORIZED;
2355+
}
2356+
}
2357+
2358+
@Tag(Tag.DIV)
2359+
public static class NullPointerExceptionHandler extends Component
2360+
implements HasErrorParameter<NullPointerException> {
2361+
2362+
@Override
2363+
public int setErrorParameter(BeforeEnterEvent event,
2364+
ErrorParameter<NullPointerException> parameter) {
2365+
return HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
2366+
}
2367+
}
2368+
2369+
@Test // spring #661
2370+
public void pick_custom_from_multiple_error_targets_when_other_is_default_annotated() {
2371+
setNavigationTargets(NpeNavigationTarget.class);
2372+
setErrorNavigationTargets(DefaultNullPointerException.class,
2373+
NullPointerExceptionHandler.class);
2374+
2375+
int result = router.navigate(ui, new Location("npe"),
23352376
NavigationTrigger.PROGRAMMATIC);
2336-
Assert.assertEquals("Non existent route should have returned.",
2337-
HttpServletResponse.SC_NOT_FOUND, result);
2377+
Assert.assertEquals(
2378+
"Null pointer should return the server error of the custom implementation.",
2379+
HttpServletResponse.SC_INTERNAL_SERVER_ERROR, result);
23382380

23392381
Assert.assertEquals(
23402382
"Expected the extending class to be used instead of the super class",
2341-
ErrorTarget.class, getUIComponent());
2383+
NullPointerExceptionHandler.class, getUIComponent());
23422384
}
23432385

23442386
@Test
23452387
public void do_not_accept_same_exception_targets() {
23462388

2347-
expectedEx.expect(InvalidRouteLayoutConfigurationException.class);
2389+
expectedEx.expect(InvalidRouteConfigurationException.class);
23482390
expectedEx.expectMessage(startsWith(
23492391
"Only one target for an exception should be defined. Found "));
23502392

0 commit comments

Comments
 (0)