UiInstanceErrorHandler.java

  1. /*
  2.  * Copyright 2010-2025 James Pether Sörling
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of 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,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  *
  16.  *  $Id$
  17.  *  $HeadURL$
  18. */
  19. package com.hack23.cia.web.impl.ui.application;

  20. import org.apache.commons.lang3.exception.ExceptionUtils;
  21. import org.slf4j.Logger;
  22. import org.slf4j.LoggerFactory;
  23. import org.springframework.security.access.AccessDeniedException;

  24. import com.hack23.cia.web.impl.ui.application.views.common.viewnames.CommonsViews;
  25. import com.vaadin.server.ErrorEvent;
  26. import com.vaadin.server.ErrorHandler;
  27. import com.vaadin.ui.Notification;
  28. import com.vaadin.ui.UI;

  29. /**
  30.  * The Class UiInstanceErrorHandler.
  31.  */
  32. public final class UiInstanceErrorHandler implements ErrorHandler {

  33.     /** The Constant LOG_WARN_VAADIN_ERROR. */
  34.     private static final String LOG_WARN_VAADIN_ERROR = "Vaadin error";

  35.     /** The Constant LOGGER. */
  36.     private static final Logger LOGGER = LoggerFactory.getLogger(UiInstanceErrorHandler.class);

  37.     /** The Constant serialVersionUID. */
  38.     private static final long serialVersionUID = 1L;

  39.     /** The ui. */
  40.     private final UI ui;

  41.     /**
  42.      * Instantiates a new ui instance error handler.
  43.      *
  44.      * @param ui
  45.      *            the ui
  46.      */
  47.     public UiInstanceErrorHandler(final UI ui) {
  48.         super();
  49.         this.ui = ui;
  50.     }

  51.     @Override
  52.     public void error(final ErrorEvent event) {
  53.         if (ExceptionUtils.getRootCause(event.getThrowable()) instanceof final AccessDeniedException accessDeniedException) {
  54.             Notification.show(accessDeniedException.getMessage(), Notification.Type.ERROR_MESSAGE);
  55.             ui.getNavigator().navigateTo(CommonsViews.MAIN_VIEW_NAME);
  56.         } else {
  57.             LOGGER.warn(LOG_WARN_VAADIN_ERROR, event.getThrowable());
  58.         }
  59.     }

  60. }