use of com.vaadin.flow.router.ErrorNavigationEvent in project flow by vaadin.
the class JavaScriptBootstrapUI method handleExceptionNavigation.
private boolean handleExceptionNavigation(Location location, Exception exception) {
Optional<ErrorTargetEntry> maybeLookupResult = getInternals().getRouter().getErrorNavigationTarget(exception);
if (maybeLookupResult.isPresent()) {
ErrorTargetEntry lookupResult = maybeLookupResult.get();
ErrorParameter<?> errorParameter = new ErrorParameter<>(lookupResult.getHandledExceptionType(), exception, exception.getMessage());
ErrorStateRenderer errorStateRenderer = new ErrorStateRenderer(new NavigationStateBuilder(getInternals().getRouter()).withTarget(lookupResult.getNavigationTarget()).build());
ErrorNavigationEvent errorNavigationEvent = new ErrorNavigationEvent(getInternals().getRouter(), location, this, NavigationTrigger.CLIENT_SIDE, errorParameter);
errorStateRenderer.handle(errorNavigationEvent);
} else {
throw new RuntimeException(exception);
}
return isPostponed();
}
use of com.vaadin.flow.router.ErrorNavigationEvent in project flow by vaadin.
the class JavaScriptBootstrapUI method handleErrorNavigation.
private void handleErrorNavigation(Location location) {
NavigationState errorNavigationState = getInternals().getRouter().resolveRouteNotFoundNavigationTarget().orElse(getDefaultNavigationError());
ErrorStateRenderer errorStateRenderer = new ErrorStateRenderer(errorNavigationState);
NotFoundException notFoundException = new NotFoundException("Couldn't find route for '" + location.getPath() + "'");
ErrorParameter<NotFoundException> errorParameter = new ErrorParameter<>(NotFoundException.class, notFoundException);
ErrorNavigationEvent errorNavigationEvent = new ErrorNavigationEvent(getInternals().getRouter(), location, this, NavigationTrigger.CLIENT_SIDE, errorParameter);
errorStateRenderer.handle(errorNavigationEvent);
}
use of com.vaadin.flow.router.ErrorNavigationEvent in project flow by vaadin.
the class ErrorStateRendererTest method handle_openNPEErrorTarget_infiniteReroute_noStackOverflow_throws.
@Test(expected = ExceptionsTrace.class)
public void handle_openNPEErrorTarget_infiniteReroute_noStackOverflow_throws() {
UI ui = configureMocks();
NavigationState state = new NavigationStateBuilder(ui.getInternals().getRouter()).withTarget(InfiniteLoopErrorTarget.class).build();
ErrorStateRenderer renderer = new ErrorStateRenderer(state);
RouteConfiguration.forRegistry(ui.getInternals().getRouter().getRegistry()).setAnnotatedRoute(InfiniteLoopNPEView.class);
ErrorParameter<Exception> parameter = new ErrorParameter<>(Exception.class, new NullPointerException());
ErrorNavigationEvent event = new ErrorNavigationEvent(ui.getInternals().getRouter(), new Location("error"), ui, NavigationTrigger.CLIENT_SIDE, parameter);
// event should route to ErrorTarget whose layout forwards to NPEView
// which reroute to ErrorTarget and this is an infinite loop
renderer.handle(event);
}
use of com.vaadin.flow.router.ErrorNavigationEvent in project flow by vaadin.
the class ErrorStateRendererTest method handle_errorViewLayoutForwardsToAView_viewIsNavigated.
@Test
public void handle_errorViewLayoutForwardsToAView_viewIsNavigated() {
UI ui = configureMocks();
NavigationState state = new NavigationStateBuilder(ui.getInternals().getRouter()).withTarget(HappyPathErrorTarget.class).build();
ErrorStateRenderer renderer = new ErrorStateRenderer(state);
RouteConfiguration.forRegistry(ui.getInternals().getRouter().getRegistry()).setAnnotatedRoute(HappyPathViewView.class);
ErrorParameter<Exception> parameter = new ErrorParameter<>(Exception.class, new NullPointerException());
ErrorNavigationEvent event = new ErrorNavigationEvent(ui.getInternals().getRouter(), new Location("error"), ui, NavigationTrigger.CLIENT_SIDE, parameter);
Assert.assertEquals(200, renderer.handle(event));
List<HasElement> chain = ui.getInternals().getActiveRouterTargetsChain();
Assert.assertEquals(1, chain.size());
Assert.assertEquals(HappyPathViewView.class, chain.get(0).getClass());
}
use of com.vaadin.flow.router.ErrorNavigationEvent in project flow by vaadin.
the class AbstractNavigationStateRenderer method getNavigationEvent.
private NavigationEvent getNavigationEvent(NavigationEvent event, BeforeEvent beforeNavigation) {
if (beforeNavigation.hasErrorParameter()) {
ErrorParameter<?> errorParameter = beforeNavigation.getErrorParameter();
return new ErrorNavigationEvent(event.getSource(), event.getLocation(), event.getUI(), NavigationTrigger.PROGRAMMATIC, errorParameter);
}
String url;
final boolean isForward = beforeNavigation.hasForwardTarget();
if (isForward) {
url = beforeNavigation.getForwardUrl();
} else {
url = beforeNavigation.getRerouteUrl();
}
if (url == null) {
final String redirectType;
final Class<? extends Component> redirectTarget;
final RouteParameters redirectParameters;
if (isForward) {
redirectType = "forward";
redirectTarget = beforeNavigation.getForwardTargetType();
redirectParameters = beforeNavigation.getForwardTargetRouteParameters();
} else {
redirectType = "reroute";
redirectTarget = beforeNavigation.getRerouteTargetType();
redirectParameters = beforeNavigation.getRerouteTargetRouteParameters();
}
throw new IllegalStateException(String.format("Attempting to %s to unresolved location target %s with route parameters %s", redirectType, redirectTarget, redirectParameters));
}
Location location = new Location(url, event.getLocation().getQueryParameters());
return new NavigationEvent(event.getSource(), location, event.getUI(), NavigationTrigger.PROGRAMMATIC, null, true);
}
Aggregations