下面列出了怎么用com.vaadin.server.ErrorEvent的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public boolean handle(ErrorEvent event, App app) {
boolean handled = super.handle(event, app);
if (handled && event.getThrowable() != null) {
// Finds the original source of the error/exception
AbstractComponent component = DefaultErrorHandler.findAbstractComponent(event);
if (component != null) {
component.markAsDirty();
}
if (component instanceof Component.Focusable) {
((Component.Focusable) component).focus();
}
}
return handled;
}
@Override
public boolean handle(ErrorEvent event, App app) {
Throwable exception = event.getThrowable();
List<Throwable> list = ExceptionUtils.getThrowableList(exception);
for (Throwable throwable : list) {
if (classNames.contains(throwable.getClass().getName())
&& canHandle(throwable.getClass().getName(), throwable.getMessage(), throwable)) {
doHandle(app, throwable.getClass().getName(), throwable.getMessage(), throwable);
return true;
}
if (throwable instanceof RemoteException) {
RemoteException remoteException = (RemoteException) throwable;
for (RemoteException.Cause cause : remoteException.getCauses()) {
if (classNames.contains(cause.getClassName())
&& canHandle(cause.getClassName(), cause.getMessage(), cause.getThrowable())) {
doHandle(app, cause.getClassName(), cause.getMessage(), cause.getThrowable());
return true;
}
}
}
}
return false;
}
@Override
public void error(final ErrorEvent event) {
// filter upload exceptions
if (event.getThrowable() instanceof UploadException) {
return;
}
final HawkbitErrorNotificationMessage message = buildNotification(getRootExceptionFrom(event));
if (event instanceof ConnectorErrorEvent) {
final Connector connector = ((ConnectorErrorEvent) event).getConnector();
if (connector instanceof UI) {
final UI uiInstance = (UI) connector;
uiInstance.access(() -> message.show(uiInstance.getPage()));
return;
}
}
final Optional<Page> originError = getPageOriginError(event);
if (originError.isPresent()) {
message.show(originError.get());
return;
}
HawkbitErrorNotificationMessage.show(message.getCaption(), message.getDescription(), Type.HUMANIZED_MESSAGE);
}
/**
* Call the session's {@link ErrorHandler}, if it has one, with the given
* exception wrapped in an {@link ErrorEvent}.
*/
private void callErrorHandler(VaadinSession session, Exception e) {
try {
ErrorHandler errorHandler = ErrorEvent.findErrorHandler(session);
if (errorHandler != null) {
errorHandler.error(new ErrorEvent(e));
}
} catch (Exception ex) {
// Let's not allow error handling to cause trouble; log fails
logger.warn("ErrorHandler call failed", ex);
}
}
@Override
public void error(final ErrorEvent event) {
if (ExceptionUtils.getRootCause(event.getThrowable()) instanceof AccessDeniedException) {
final AccessDeniedException accessDeniedException = (AccessDeniedException) ExceptionUtils.getRootCause(event.getThrowable());
Notification.show(accessDeniedException.getMessage(), Notification.Type.ERROR_MESSAGE);
ui.getNavigator().navigateTo(CommonsViews.MAIN_VIEW_NAME);
} else {
LOGGER.warn(LOG_WARN_VAADIN_ERROR, event.getThrowable());
}
}
public static void doDefault(ErrorEvent event) {
Throwable t = event.getThrowable();
if (t instanceof SocketException) {
// Most likely client browser closed socket
getLogger().info(
"SocketException in CommunicationManager."
+ " Most likely client (browser) closed socket.");
return;
}
t = findRelevantThrowable(t);
/*
* Handle SpringSecurity
*/
if (t instanceof AccessDeniedException) {
EventBus eventBus = SpringApplicationContext.getEventBus();
eventBus.publish(EventScope.UI, eventBus, new AccessDeniedEvent(t));
getLogger().log(Level.FINE, "Access is denied", t);
return;
}
// Finds the original source of the error/exception
AbstractComponent component = findAbstractComponent(event);
if (component != null) {
// Shows the error in AbstractComponent
ErrorMessage errorMessage = AbstractErrorMessage
.getErrorMessageForException(t);
component.setComponentError(errorMessage);
}
// also print the error on console
getLogger().log(Level.SEVERE, "", t);
}
/**
* Returns the AbstractComponent associated with the given error if such can
* be found
*
* @param event
* The error to investigate
* @return The {@link AbstractComponent} to error relates to or null if
* could not be determined or if the error does not relate to any
* AbstractComponent.
*/
public static AbstractComponent findAbstractComponent(
com.vaadin.server.ErrorEvent event) {
if (event instanceof ConnectorErrorEvent) {
Component c = findComponent(((ConnectorErrorEvent) event)
.getConnector());
if (c instanceof AbstractComponent) {
return (AbstractComponent) c;
}
}
return null;
}
@Override
public void error(ErrorEvent event) {
Throwable t = findRelevantThrowable(event.getThrowable());
if (isSecurityException(t)) {
handleSecurityException(t);
return;
}
super.error(event);
}
@Override
public boolean handle(ErrorEvent event, App app) {
// Copied from com.vaadin.server.DefaultErrorHandler.doDefault()
Throwable t = event.getThrowable();
if (t instanceof SocketException
|| ExceptionUtils.getRootCause(t) instanceof SocketException) {
// Most likely client browser closed socket
return true;
}
// if it is UberJar or deployed to Jetty
if (ExceptionUtils.getThrowableList(t).stream()
.anyMatch(o -> o.getClass().getName().equals("org.eclipse.jetty.io.EofException"))) {
// Most likely client browser closed socket
return true;
}
// Support Tomcat 8 ClientAbortException
if (StringUtils.contains(ExceptionUtils.getMessage(t), "ClientAbortException")) {
// Most likely client browser closed socket
return true;
}
AppUI ui = AppUI.getCurrent();
if (ui == null) {
// there is no UI, just add error to log
return true;
}
if (t != null) {
if (app.getConnection().getSession() != null) {
showDialog(app, ui, t);
} else {
showNotification(app, ui, t);
}
}
// default handler always return true
return true;
}
private static Throwable getRootExceptionFrom(final ErrorEvent event) {
return getRootCauseOf(event.getThrowable());
}
@Override
public void error(ErrorEvent event) {
doDefault(event);
}
private static Optional<Page> getPageOriginError(final ErrorEvent event) {
final Component errorOrigin = findAbstractComponent(event);
if (errorOrigin != null && errorOrigin.getUI() != null) {
return Optional.ofNullable(errorOrigin.getUI().getPage());
}
return Optional.empty();
}
/**
* Handle an exception. Implementation class should either handle the exception and return true, or return false
* to delegate execution to the next handler in the chain of responsibility.
* @param event error event containing the exception, generated by Vaadin
* @param app current {@link App} instance
* @return true if the exception has been successfully handled, false if not
*/
boolean handle(ErrorEvent event, App app);