3

我希望框架能够捕获每个未捕获的异常并显示适当的通知消息。

因此,我ErrorHandler在我的 UI 中设置了这样的自定义(受“Vaadin 之书”的启发:https ://vaadin.com/book/-/page/application.errors.html ):

public abstract class MyUI extends UI {

    @Override
    protected void init(final VaadinRequest request) {
        setErrorHandler(new DefaultErrorHandler() {
            @Override
            public void error(final com.vaadin.server.ErrorEvent event) {
                Throwable error = event.getThrowable();
                Notification.show(error.getLocalizedMessage(), Notification.Type.ERROR_MESSAGE);
            }
        });
    }
}

但是当我从后端加载时引发错误时,它不会执行。相反,表单显示不完整,当我重新加载页面 (F5) 时,我得到一个异常视图,就像我从 Tomcat 的一般异常处理程序中知道的那样。

是不是我设置错了ErrorHandler?有没有更好的办法?

我正在使用 Glassfish 4 和 Vaadin 7。

4

1 回答 1

5

我们还使用 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());
    }

    ...
}
于 2013-12-17T07:42:25.247 回答