Steffen Harbich
Steffen Harbich

Reputation: 2759

How to handle exceptions thrown in DataProvider methods centrally

When a DataProvider fetch or count method throws an exception, e.g. because the user is not authorized, how could I handle these exceptions centrally? I know there is HasErrorParameter interface to show error views when there is an exception thrown when routing. But these error views are not triggered when DataProvider throws the exception.

Example:

  new AbstractBackEndDataProvider<String, Void>() {
        @Override
        protected Stream<String> fetchFromBackEnd(Query<String, Void> query) {
            ...
        }

        @Override
        protected int sizeInBackEnd(Query<String, Void> query) {
            throw new UnsupportedOperationException("test");
        }
    }

@Route("failed")
public class FailView extends VerticalLayout 
         implements HasErrorParameter<UnsupportedOperationException> {...}

Even if I do a try catch within the DataProvider methods, I don't see how I could navigate to the appropriate error view just by using the caught exception and not the view component class (this wouldn't trigger setErrorParameter method).

BTW: I miss the router exception handling topic in Vaadin Flow 13 documentation. I wonder why they removed it.

Upvotes: 2

Views: 1427

Answers (1)

froemijojo
froemijojo

Reputation: 141

I believe all Exceptions that don't occur while routing will be given to the ErrorHandler of the VaadinSession the error occured in.

The best way to set the ErrorHandler seems to be to override the sessionInit method in a custom SessionInitListener

You can add a custom SessionInitListener inside the servletInitialized method of a custom VaadinServlet.

class CustomServlet extends VaadinServlet{
    @Override
    protected void servletInitialized() throws ServletException {
        super.servletInitialized();
        getService().addSessionInitListener(new CustomSessionInitListener());
    }
}

And that SessionInitListener (in this example CustomSessionInitListener) has to set the errorHandler of the sessions that get initialized.

class CustomSessionInitListener implements SessionInitListener{
    @Override
    public void sessionInit(SessionInitEvent event) throws ServiceException {
        event.getSession().setErrorHandler(new CustomErrorHandler());
    }
}

For further information on how to create your own Servlet take a look at Vaadin's tutorial page(you need to scroll down to "Customizing Vaadin Servlet")

Edit: To show the error page you need to get Vaadin to reroute to an error. To achieve that we can use an BeforeEnterEvent, BeforeEnterEvents have a rerouteToError method which we can use to let Vaadin show our ErrorView.

But we also want to pass along the Exception instance, so we have to store that as well. I did exactly that with the following class:

@Route("error-view") // Route shown in the user's browser
public class ErrorViewShower extends Div implements BeforeEnterObserver {
    // Class to store the current Exception of each UI in
    private static class UIExceptionContainer extends HashMap<UI, Exception> {

    }

    // Method to call when we want to show an error
    public static void showError(Exception exception) {
        UIExceptionContainer exceptionContainer = VaadinSession.getCurrent().getAttribute(UIExceptionContainer.class);
        // Creating and setting the exceptionContainer in case it hasn't been set yet.
        if (exceptionContainer == null) {
            exceptionContainer = new UIExceptionContainer();
            VaadinSession.getCurrent().setAttribute(UIExceptionContainer.class, exceptionContainer);
        }

        // Storing the exception for the beforeEnter method
        exceptionContainer.put(UI.getCurrent(), exception);

        // Now we navigate to an Instance of this class, to use the BeforeEnterEvent to reroute to the actual error view
        UI.getCurrent().navigate(ErrorViewShower.class);// If this call doesn't work you might want to wrap into UI.access
    }

    @Override
    public void beforeEnter(BeforeEnterEvent event) {
        UIExceptionContainer exceptionContainer = VaadinSession.getCurrent().getAttribute(UIExceptionContainer.class);

        // Retrieving the previously stored exception. You might want to handle if this has been called without setting any Exception.
        Exception exception = exceptionContainer.get(UI.getCurrent());

        //Clearing out the now handled Exception
        exceptionContainer.remove(UI.getCurrent());

        // Using the BeforeEnterEvent to show the error
        event.rerouteToError(exception, "Possible custom message for the ErrorHandler here");
    }

}

Usage of it in combination with the error handler looks like this:

public class CustomErrorHandler implements ErrorHandler {
    @Override
    public void error(ErrorEvent event) {
        // This can easily throw an exception itself, you need to add additional checking before casting.
        // And it's possible that this method is called outside the context of an UI(when a dynamic resource throws an exception for example)
        Exception exception = (Exception) event.getThrowable();
        ErrorViewShower.showError(exception);
    }

}

Edit2: As it turns out that Exceptions occuring inside internal method calls don't get handled by the UI's ErrorHandler or the VaadinSession's ErrorHandler but instead by another error handler which causes the client side to terminate and show the Error Notification,

a solution is to catch the Exceptions inside the methods of the DataProvider and pass them to ErrorViewShower.showError() and still return without any Exception flying the stacktrace upwards. (Or don't throw any Exception yourself and instead simply pass a new to the ErrorViewShower.showError() method).

By returning normally Vaadin doesn't even know something went wrong.
ErrorViewShower.showError() calls ui.navigate, that navigation command seems to get "queued" behind the calls to the DataProvider, meaning the view of the user will change in the same request.

Dataprovider with such an implementation:

new AbstractBackEndDataProvider<String, Void>() {
    @Override
    protected Stream<String> fetchFromBackEnd(Query<String, Void> query) {
        try{
            //Code that can throw an Exception here
        }catch(Exception e){
            ErrorViewShower.showError(e);
            //We have to make sure that query.getLimit and query.getOffset gets called, otherwise Vaadin throws an Exception with the message "the data provider hasn't ever called getLimit() method on the provided query. It means that the the data provider breaks the contract and the returned stream contains unxpected data."
            query.getLimit();
            query.getOffset();
            return Stream.of(); //Stream of empty Array to return without error
        }
    }

    @Override
    protected int sizeInBackEnd(Query<String, Void> query) {
        //Second way i mentioned, but this will not catch any Exception you didn't create, where as the try...catch has no way to let any Exception reach Vaadin.
        if(badThingsHappened){
            ErrorViewShower.showError(new UnsupportedOperationException("Bad things..."));
            return 0;//Exiting without error
        }
    }
}

Upvotes: 4

Related Questions