Search in sources :

Example 1 with ErrorTargetEntry

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);
    }
}
Also used : ErrorTargetEntry(com.vaadin.flow.router.internal.ErrorTargetEntry) ErrorStateRenderer(com.vaadin.flow.router.internal.ErrorStateRenderer)

Example 2 with ErrorTargetEntry

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();
}
Also used : NavigationStateBuilder(com.vaadin.flow.router.NavigationStateBuilder) ErrorTargetEntry(com.vaadin.flow.router.internal.ErrorTargetEntry) ErrorNavigationEvent(com.vaadin.flow.router.ErrorNavigationEvent) ErrorStateRenderer(com.vaadin.flow.router.internal.ErrorStateRenderer) ErrorParameter(com.vaadin.flow.router.ErrorParameter)

Example 3 with ErrorTargetEntry

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);
    }
}
Also used : ErrorTargetEntry(com.vaadin.flow.router.internal.ErrorTargetEntry) ErrorStateRenderer(com.vaadin.flow.router.internal.ErrorStateRenderer)

Example 4 with ErrorTargetEntry

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;
}
Also used : VaadinSession(com.vaadin.flow.server.VaadinSession) RouteRegistry(com.vaadin.flow.server.RouteRegistry) HashMap(java.util.HashMap) NotFoundException(com.vaadin.flow.router.NotFoundException) Page(com.vaadin.flow.component.page.Page) HttpServletRequest(javax.servlet.http.HttpServletRequest) UI(com.vaadin.flow.component.UI) NavigationEvent(com.vaadin.flow.router.NavigationEvent) HttpSession(javax.servlet.http.HttpSession) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) Router(com.vaadin.flow.router.Router) UIInternals(com.vaadin.flow.component.internal.UIInternals) BeforeEnterEvent(com.vaadin.flow.router.BeforeEnterEvent) ErrorTargetEntry(com.vaadin.flow.router.internal.ErrorTargetEntry) RouteNotFoundError(com.vaadin.flow.router.RouteNotFoundError) Principal(java.security.Principal) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration) Location(com.vaadin.flow.router.Location)

Example 5 with ErrorTargetEntry

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());
}
Also used : ErrorTargetEntry(com.vaadin.flow.router.internal.ErrorTargetEntry) NotFoundException(com.vaadin.flow.router.NotFoundException) ServletException(javax.servlet.ServletException) InvalidRouteLayoutConfigurationException(com.vaadin.flow.server.InvalidRouteLayoutConfigurationException) NotFoundException(com.vaadin.flow.router.NotFoundException) ExpectedException(org.junit.rules.ExpectedException) InvalidRouteConfigurationException(com.vaadin.flow.server.InvalidRouteConfigurationException) Test(org.junit.Test)

Aggregations

ErrorTargetEntry (com.vaadin.flow.router.internal.ErrorTargetEntry)5 ErrorStateRenderer (com.vaadin.flow.router.internal.ErrorStateRenderer)3 NotFoundException (com.vaadin.flow.router.NotFoundException)2 UI (com.vaadin.flow.component.UI)1 UIInternals (com.vaadin.flow.component.internal.UIInternals)1 Page (com.vaadin.flow.component.page.Page)1 DeploymentConfiguration (com.vaadin.flow.function.DeploymentConfiguration)1 BeforeEnterEvent (com.vaadin.flow.router.BeforeEnterEvent)1 ErrorNavigationEvent (com.vaadin.flow.router.ErrorNavigationEvent)1 ErrorParameter (com.vaadin.flow.router.ErrorParameter)1 Location (com.vaadin.flow.router.Location)1 NavigationEvent (com.vaadin.flow.router.NavigationEvent)1 NavigationStateBuilder (com.vaadin.flow.router.NavigationStateBuilder)1 RouteNotFoundError (com.vaadin.flow.router.RouteNotFoundError)1 Router (com.vaadin.flow.router.Router)1 InvalidRouteConfigurationException (com.vaadin.flow.server.InvalidRouteConfigurationException)1 InvalidRouteLayoutConfigurationException (com.vaadin.flow.server.InvalidRouteLayoutConfigurationException)1 RouteRegistry (com.vaadin.flow.server.RouteRegistry)1 VaadinServletRequest (com.vaadin.flow.server.VaadinServletRequest)1 VaadinSession (com.vaadin.flow.server.VaadinSession)1