我们还使用 Glassfish 4 和 Vaadin 7。您可以像我们一样编写自己的自定义 ErrorHandler(实现接口com.vaadin.server.ErrorHandler
):
@Override
public void error(ErrorEvent event) {
// Finds the original source of the error/exception
AbstractComponent component = DefaultErrorHandler.findAbstractComponent(event);
if (component != null) {
ErrorMessage errorMessage = getErrorMessageForException(event.getThrowable());
if (errorMessage != null) {
component.setComponentError(errorMessage);
new Notification(null, errorMessage.getFormattedHtmlMessage(), Type.WARNING_MESSAGE, true).show(Page.getCurrent());
return;
}
}
DefaultErrorHandler.doDefault(event);
}
方法 getErrorMessageForException 找出通常有用的主要原因:
private static ErrorMessage getErrorMessageForException(Throwable t) {
PersistenceException persistenceException = getCauseOfType(t, PersistenceException.class);
if (persistenceException != null) {
return new UserError(persistenceException.getLocalizedMessage(), AbstractErrorMessage.ContentMode.TEXT, ErrorMessage.ErrorLevel.ERROR);
}
SQLException sqlException = getCauseOfType(t, SQLException.class);
if (sqlException != null) {
return new SQLErrorMessage(sqlException);
}
FieldGroup.CommitException commitException = getCauseOfType(t, FieldGroup.CommitException.class);
if (commitException != null) {
return new CommitErrorMessage(commitException);
}
EJBException eJBException = getCauseOfType(t, EJBException.class);
if (eJBException != null) {
return new UserError(eJBException.getLocalizedMessage(), AbstractErrorMessage.ContentMode.TEXT, ErrorMessage.ErrorLevel.ERROR);
}
...
}
private static <T extends Throwable> T getCauseOfType(Throwable th, Class<T> type) {
while (th != null) {
if (type.isAssignableFrom(th.getClass())) {
return (T) th;
} else {
th = th.getCause();
}
}
return null;
}
希望这可以帮助您找到适合您的解决方案。
编辑:关于设置ErrorHandler的问题和提示:
import com.vaadin.annotations.Theme;
import com.vaadin.cdi.CDIUI;
import com.vaadin.ui.UI;
@CDIUI
@Theme("abc")
public class CustomUI
extends UI {
...
@Override
protected void init(VaadinRequest request) {
...
// at main UI ...
UI.getCurrent().setErrorHandler(new CustomErrorHandler());
// ... or on session level
VaadinSession.getCurrent().setErrorHandler(new CustomErrorHandler());
}
...
}