Beautify error notification.

Signed-off-by: Melanie Retter <melanie.retter@bosch-si.com>
This commit is contained in:
Melanie Retter
2016-07-28 15:23:56 +02:00
parent 2b79cafb9e
commit 27019b9e4c
8 changed files with 103 additions and 128 deletions

View File

@@ -30,10 +30,7 @@ import com.vaadin.ui.VerticalLayout;
* View class that is instantiated when no other view matches the navigation * View class that is instantiated when no other view matches the navigation
* state. * state.
* *
*
*
* @see Navigator#setErrorView(Class) * @see Navigator#setErrorView(Class)
*
*/ */
@SuppressWarnings("serial") @SuppressWarnings("serial")
@SpringComponent @SpringComponent

View File

@@ -14,7 +14,7 @@ import java.util.Set;
import javax.servlet.http.Cookie; import javax.servlet.http.Cookie;
import org.eclipse.hawkbit.ui.components.SPUIErrorHandler; import org.eclipse.hawkbit.ui.components.HawkbitUIErrorHandler;
import org.eclipse.hawkbit.ui.menu.DashboardEvent.PostViewChangeEvent; import org.eclipse.hawkbit.ui.menu.DashboardEvent.PostViewChangeEvent;
import org.eclipse.hawkbit.ui.menu.DashboardMenu; import org.eclipse.hawkbit.ui.menu.DashboardMenu;
import org.eclipse.hawkbit.ui.menu.DashboardMenuItem; import org.eclipse.hawkbit.ui.menu.DashboardMenuItem;
@@ -180,7 +180,10 @@ public class HawkbitUI extends DefaultHawkbitUI implements DetachListener {
final String locale = getLocaleId(SPUIDefinitions.getAvailableLocales()); final String locale = getLocaleId(SPUIDefinitions.getAvailableLocales());
setLocale(new Locale(locale)); setLocale(new Locale(locale));
UI.getCurrent().setErrorHandler(new SPUIErrorHandler()); if (UI.getCurrent().getErrorHandler() == null) {
UI.getCurrent().setErrorHandler(new HawkbitUIErrorHandler());
}
LOG.info("Current locale of the application is : {}", i18n.getLocale()); LOG.info("Current locale of the application is : {}", i18n.getLocale());
} }

View File

@@ -10,17 +10,13 @@ package org.eclipse.hawkbit.ui.components;
import org.eclipse.hawkbit.ui.utils.SPUILabelDefinitions; import org.eclipse.hawkbit.ui.utils.SPUILabelDefinitions;
import com.vaadin.server.Page;
import com.vaadin.shared.Position; import com.vaadin.shared.Position;
import com.vaadin.ui.Notification; import com.vaadin.ui.Notification;
/** /**
* Notification message component. * Notification message component.
*
*
*
*/ */
public class SPNotificationMessage extends Notification { public class HawkbitNotificationMessage extends Notification {
/** /**
* ID. * ID.
@@ -30,30 +26,10 @@ public class SPNotificationMessage extends Notification {
/** /**
* Constructor. * Constructor.
*/ */
public SPNotificationMessage() { public HawkbitNotificationMessage() {
super(""); super("");
} }
/**
* Notification message component.
*
* @param styleName
* style name of message
* @param caption
* message caption
* @param description
* message description
* @param autoClose
* flag to indicate enable close option
* @param page
* current {@link Page}
*/
public void showNotification(final String styleName, final String caption, final String description,
final Boolean autoClose, final Page page) {
decorate(styleName, caption, description, autoClose);
this.show(page);
}
/** /**
* Decorate. * Decorate.
* *
@@ -66,7 +42,7 @@ public class SPNotificationMessage extends Notification {
* @param autoClose * @param autoClose
* flag to indicate enable close option * flag to indicate enable close option
*/ */
private void decorate(final String styleName, final String caption, final String description, public void decorateWith(final String styleName, final String caption, final String description,
final Boolean autoClose) { final Boolean autoClose) {
setCaption(caption); setCaption(caption);
setDescription(description); setDescription(description);

View File

@@ -0,0 +1,84 @@
/**
* Copyright (c) 2015 Bosch Software Innovations GmbH and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.eclipse.hawkbit.ui.components;
import static com.vaadin.ui.themes.ValoTheme.NOTIFICATION_CLOSABLE;
import static com.vaadin.ui.themes.ValoTheme.NOTIFICATION_FAILURE;
import static com.vaadin.ui.themes.ValoTheme.NOTIFICATION_SMALL;
import org.eclipse.hawkbit.ui.utils.I18N;
import org.eclipse.hawkbit.ui.utils.SpringContextHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.base.Optional;
import com.vaadin.server.DefaultErrorHandler;
import com.vaadin.server.ErrorEvent;
import com.vaadin.server.Page;
import com.vaadin.ui.Component;
/**
* Default handler for SP UI.
*/
public class HawkbitUIErrorHandler extends DefaultErrorHandler {
private static final long serialVersionUID = 1L;
private static final Logger LOG = LoggerFactory.getLogger(HawkbitUIErrorHandler.class);
private static final String STYLE = NOTIFICATION_FAILURE + " " + NOTIFICATION_SMALL + " " + NOTIFICATION_CLOSABLE;
@Override
public void error(final ErrorEvent event) {
LOG.error("Error in UI: ", event.getThrowable());
final Optional<Page> originError = getPageOriginError(event);
if (originError.isPresent()) {
final HawkbitNotificationMessage message = buildNotification(getRootExceptionFrom(event));
message.show(originError.get());
}
}
private Throwable getRootExceptionFrom(final ErrorEvent event) {
return getRootCauseOf(event.getThrowable());
}
private Throwable getRootCauseOf(final Throwable exception) {
if (exception.getCause() != null) {
return getRootCauseOf(exception.getCause());
}
return exception;
}
private Optional<Page> getPageOriginError(final ErrorEvent event) {
final Component errorOrigin = findAbstractComponent(event);
if (errorOrigin != null && errorOrigin.getUI() != null) {
return Optional.fromNullable(errorOrigin.getUI().getPage());
}
return Optional.absent();
}
protected HawkbitNotificationMessage buildNotification(final Throwable exception) {
final HawkbitNotificationMessage notification = new HawkbitNotificationMessage();
final I18N i18n = SpringContextHelper.getBean(I18N.class);
notification.decorateWith(STYLE, i18n.get("caption.error"),
i18n.get("message.error.temp", exception.getClass().getSimpleName()), false);
return notification;
}
}

View File

@@ -1,65 +0,0 @@
/**
* Copyright (c) 2015 Bosch Software Innovations GmbH and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.eclipse.hawkbit.ui.components;
import org.eclipse.hawkbit.ui.utils.HawkbitCommonUtil;
import org.eclipse.hawkbit.ui.utils.I18N;
import org.eclipse.hawkbit.ui.utils.SpringContextHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.vaadin.server.DefaultErrorHandler;
import com.vaadin.server.ErrorEvent;
import com.vaadin.ui.Component;
import com.vaadin.ui.themes.ValoTheme;
/**
*
* Default handler for SP UI.
*
*
*
*
*
*/
public class SPUIErrorHandler extends DefaultErrorHandler {
/**
* Comment for <code>serialVersionUID</code>.
*/
private static final long serialVersionUID = 1877326479308824191L;
/**
* logger.
*/
private static final Logger LOG = LoggerFactory.getLogger(SPUIErrorHandler.class);
@Override
public void error(final ErrorEvent event) {
final SPNotificationMessage notification = new SPNotificationMessage();
// Build error style
final StringBuilder style = new StringBuilder(ValoTheme.NOTIFICATION_FAILURE);
style.append(' ');
style.append(ValoTheme.NOTIFICATION_SMALL);
style.append(' ');
style.append(ValoTheme.NOTIFICATION_CLOSABLE);
final I18N i18n = SpringContextHelper.getBean(I18N.class);
String exceptionName = null;
// From the exception trace we get the expected exception class name
for (Throwable error = event.getThrowable(); error != null; error = error.getCause()) {
exceptionName = HawkbitCommonUtil.getLastSequenceBySplitByDot(error.getClass().getName());
LOG.error("Error in SP-UI:", error);
}
final Component errorOrgin = findAbstractComponent(event);
if (null != errorOrgin && errorOrgin.getUI() != null) {
notification.showNotification(style.toString(), i18n.get("caption.error"),
i18n.get("message.error.temp", new Object[] { exceptionName }), false,
errorOrgin.getUI().getPage());
}
}
}

View File

@@ -590,24 +590,6 @@ public final class HawkbitCommonUtil {
return requiredExtraWidth + minTableWidth; return requiredExtraWidth + minTableWidth;
} }
/**
* get the Last sequence of string which is after last dot in String.
*
* @param name
* dotted String name
* @return String name
*/
public static String getLastSequenceBySplitByDot(final String name) {
String lastSequence = null;
if (null != name) {
final String[] strArray = name.split("\\.");
if (strArray.length > 0) {
lastSequence = strArray[strArray.length - 1];
}
}
return lastSequence;
}
/** /**
* Remove the prefix from text. * Remove the prefix from text.
* *

View File

@@ -54,6 +54,7 @@ public final class SPUILabelDefinitions {
*/ */
public static final String SP_NOTIFICATION_ERROR_MESSAGE_STYLE = ValoTheme.NOTIFICATION_ERROR + " " public static final String SP_NOTIFICATION_ERROR_MESSAGE_STYLE = ValoTheme.NOTIFICATION_ERROR + " "
+ ValoTheme.NOTIFICATION_TRAY; + ValoTheme.NOTIFICATION_TRAY;
/** /**
* Style - Warning. * Style - Warning.
*/ */
@@ -540,7 +541,6 @@ public final class SPUILabelDefinitions {
*/ */
public static final String VAR_TOTAL_TARGETS = "totalTargetsCount"; public static final String VAR_TOTAL_TARGETS = "totalTargetsCount";
/** /**
* Total target count status coulmn property name. * Total target count status coulmn property name.
*/ */

View File

@@ -18,8 +18,6 @@ import com.vaadin.spring.annotation.ViewScope;
/** /**
* Show success and error messages. * Show success and error messages.
*
*
*/ */
@ViewScope @ViewScope
@SpringComponent @SpringComponent
@@ -33,8 +31,6 @@ public class UINotification implements Serializable {
/** /**
* Display success type of notification message. * Display success type of notification message.
* *
* @param notificationMessage
* as reference
* @param message * @param message
* is the message to displayed as success. * is the message to displayed as success.
*/ */
@@ -46,8 +42,6 @@ public class UINotification implements Serializable {
/** /**
* Display error type of notification message. * Display error type of notification message.
* *
* @param notificationMessage
* as reference
* @param message * @param message
* as message. * as message.
*/ */
@@ -62,9 +56,13 @@ public class UINotification implements Serializable {
/** /**
* Display error type of notification message. * Display error type of notification message.
* *
* @param message. * @param message
* @param caption. * errorMessage
* @param autoClose. * @param caption
* caption of the errorMessage
* @param autoClose
* boolean, if errorMessage should be closed by clicking on the
* error (false) or by clicking anywhere (true)
*/ */
public void displayError(final String message, final String caption, final Boolean autoClose) { public void displayError(final String message, final String caption, final Boolean autoClose) {
final StringBuilder updatedMsg = new StringBuilder(FontAwesome.EXCLAMATION_TRIANGLE.getHtml()); final StringBuilder updatedMsg = new StringBuilder(FontAwesome.EXCLAMATION_TRIANGLE.getHtml());