Andy
Andy

Reputation: 1994

Central error handling in Vaadin

I want the framework to catch every uncaught exception and show an appropriate notification message.

Therefore I set a custom ErrorHandler in my UI like this (inspired by the "Book of 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);
            }
        });
    }
}

But it is not executed when I provoke an error on loading from backend. Instead the form is shown incompletely, and when i reload the page (F5) I get an exception view like the one I know from Tomcat's general exception handlers.

Is it wrong as I set the ErrorHandler? Is there a better way?

I'm using Glassfish 4 and Vaadin 7.

Upvotes: 3

Views: 6584

Answers (1)

Mayoares
Mayoares

Reputation: 1284

We also use Glassfish 4 and Vaadin 7. You could write your own customized ErrorHandler (which implements the interface com.vaadin.server.ErrorHandler) as we do:

@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);
}

The method getErrorMessageForException finds out the main cause which is often useful:

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;
}

Hopefully this helps you a bit to find a good solution for you.

EDIT: Regarding question and hint where to set the 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());
    }

    ...
}

Upvotes: 6

Related Questions