use of com.vaadin.flow.router.internal.ErrorTargetEntry in project flow by vaadin.
the class BeforeEvent method rerouteToError.
/**
* Reroute to error target for given exception with given custom message.
*
* @param exception
* exception to get error target for
* @param customMessage
* custom message to send to error target
*/
public void rerouteToError(Exception exception, String customMessage) {
Optional<ErrorTargetEntry> maybeLookupResult = getSource().getErrorNavigationTarget(exception);
if (maybeLookupResult.isPresent()) {
ErrorTargetEntry lookupResult = maybeLookupResult.get();
rerouteTargetState = new NavigationStateBuilder(ui.getInternals().getRouter()).withTarget(lookupResult.getNavigationTarget()).build();
rerouteTarget = new ErrorStateRenderer(rerouteTargetState);
errorParameter = new ErrorParameter<>(lookupResult.getHandledExceptionType(), exception, customMessage);
} else {
throw new RuntimeException(customMessage, exception);
}
}
use of com.vaadin.flow.router.internal.ErrorTargetEntry 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.internal.ErrorTargetEntry in project flow by vaadin.
the class Router method handleExceptionNavigation.
private int handleExceptionNavigation(UI ui, Location location, Exception exception, NavigationTrigger trigger, JsonValue state) {
Optional<ErrorTargetEntry> maybeLookupResult = getErrorNavigationTarget(exception);
if (maybeLookupResult.isPresent()) {
ErrorTargetEntry lookupResult = maybeLookupResult.get();
ErrorParameter<?> errorParameter = new ErrorParameter<>(lookupResult.getHandledExceptionType(), exception, exception.getMessage());
ErrorStateRenderer handler = new ErrorStateRenderer(new NavigationStateBuilder(this).withTarget(lookupResult.getNavigationTarget()).build());
ErrorNavigationEvent navigationEvent = new ErrorNavigationEvent(this, location, ui, trigger, errorParameter, state);
return handler.handle(navigationEvent);
} else {
throw new RuntimeException(exception);
}
}
use of com.vaadin.flow.router.internal.ErrorTargetEntry in project flow by vaadin.
the class ViewAccessCheckerTest method setupRequest.
private Result setupRequest(Class navigationTarget, User user, boolean productionMode) {
CurrentInstance.clearAll();
Principal principal;
String[] roles;
if (user == User.USER_NO_ROLES) {
principal = AccessAnnotationCheckerTest.USER_PRINCIPAL;
roles = new String[0];
} else if (user == User.NORMAL_USER) {
principal = AccessAnnotationCheckerTest.USER_PRINCIPAL;
roles = new String[] { "user" };
} else if (user == User.ADMIN) {
principal = AccessAnnotationCheckerTest.USER_PRINCIPAL;
roles = new String[] { "admin" };
} else {
principal = null;
roles = new String[0];
}
VaadinServletRequest vaadinServletRequest = Mockito.mock(VaadinServletRequest.class);
HttpServletRequest httpServletRequest = AccessAnnotationCheckerTest.createRequest(principal, roles);
Mockito.when(vaadinServletRequest.getHttpServletRequest()).thenReturn(httpServletRequest);
CurrentInstance.set(VaadinRequest.class, vaadinServletRequest);
Router router = Mockito.mock(Router.class);
UI ui = Mockito.mock(UI.class);
Page page = Mockito.mock(Page.class);
Mockito.when(ui.getPage()).thenReturn(page);
VaadinSession vaadinSession = Mockito.mock(VaadinSession.class);
Mockito.when(ui.getSession()).thenReturn(vaadinSession);
DeploymentConfiguration configuration = Mockito.mock(DeploymentConfiguration.class);
Mockito.when(vaadinSession.getConfiguration()).thenReturn(configuration);
Mockito.when(configuration.isProductionMode()).thenReturn(productionMode);
UIInternals uiInternals = Mockito.mock(UIInternals.class);
Mockito.when(ui.getInternals()).thenReturn(uiInternals);
Mockito.when(uiInternals.getRouter()).thenReturn(router);
Mockito.when(router.getErrorNavigationTarget(Mockito.any())).thenAnswer(invocation -> {
Class<?> exceptionClass = invocation.getArguments()[0].getClass();
if (exceptionClass == NotFoundException.class) {
return Optional.of(new ErrorTargetEntry(RouteNotFoundError.class, NotFoundException.class));
} else {
return Optional.empty();
}
});
Location location = new Location(getRoute(navigationTarget));
NavigationEvent navigationEvent = new NavigationEvent(router, location, ui, NavigationTrigger.ROUTER_LINK);
BeforeEnterEvent event = new BeforeEnterEvent(navigationEvent, navigationTarget, new ArrayList<>());
RouteRegistry routeRegistry = Mockito.mock(RouteRegistry.class);
Mockito.when(router.getRegistry()).thenReturn(routeRegistry);
Mockito.when(routeRegistry.getNavigationTarget(Mockito.anyString())).thenAnswer(invocation -> {
String url = (String) invocation.getArguments()[0];
if (location.getPath().equals(url)) {
return Optional.of(navigationTarget);
} else {
return Optional.empty();
}
});
HttpSession session = Mockito.mock(HttpSession.class);
Map<String, Object> sessionAttributes = new HashMap<>();
Mockito.when(httpServletRequest.getSession()).thenReturn(session);
Mockito.doAnswer(invocation -> {
String key = (String) invocation.getArguments()[0];
Object value = invocation.getArguments()[1];
sessionAttributes.put(key, value);
return null;
}).when(session).setAttribute(Mockito.anyString(), Mockito.any());
Result info = new Result();
info.event = event;
info.sessionAttributes = sessionAttributes;
Mockito.doAnswer(invocation -> {
info.redirectUsingPageLocation = (String) invocation.getArguments()[0];
return null;
}).when(page).setLocation(Mockito.anyString());
return info;
}
use of com.vaadin.flow.router.internal.ErrorTargetEntry in project flow by vaadin.
the class RouteRegistryInitializerTest method routeFilter_ignoresErrorTargets.
@Test
public void routeFilter_ignoresErrorTargets() throws InvalidRouteConfigurationException {
registry.setErrorNavigationTargets(Stream.of(IgnoredErrorView.class, FileNotFound.class).collect(Collectors.toSet()));
Assert.assertTrue(registry.getErrorNavigationTarget(new NotFoundException()).isPresent());
ErrorTargetEntry errorTargetEntry = registry.getErrorNavigationTarget(new Exception()).get();
Assert.assertNotEquals(IgnoredErrorView.class, errorTargetEntry.getNavigationTarget());
}
Aggregations