我希望框架能够捕获每个未捕获的异常并显示适当的通知消息.
因此,我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);
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我从后端加载错误时它没有被执行.相反,表单显示不完整,当我重新加载页面(F5)时,我得到一个异常视图,就像我从Tomcat的常规异常处理程序中知道的那样.
我设置它是不对的ErrorHandler?有没有更好的办法?
我正在使用Glassfish 4和Vaadin 7.
我们还使用Glassfish 4和Vaadin 7.您可以com.vaadin.server.ErrorHandler像我们一样编写自己的自定义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);
}
Run Code Online (Sandbox Code Playgroud)
方法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;
}
Run Code Online (Sandbox Code Playgroud)
希望这有助于您找到一个好的解决方案.
编辑:关于问题和提示在哪里设置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());
}
...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6099 次 |
| 最近记录: |