Exceptions from FileDownloader not showing up in ErrorHandler?

I’m currently using Vaadin 7.1.8.

In the UI class for my Vaadin application, I’ve defined a new ErrorHandler for


UI.getCurrent().setErrorHandler(new MyErrorHandler());

MyErrorHandler implements ErrorHandler and should show all uncaught exceptions, basically with parsing the exception as described in the book of Vaadin 4.5.3 (https://vaadin.com/book/-/page/application.errors.html) and displaying the message from the exception with



new Notification(“Error”, message, Notification.Type.ERROR_MESSAGE).show(Page.getCurrent());

This usually works fine and displays the error message.

Recently, I’ve started working with the com.vaadin.server.FileDownloader class. Attaching it to a component (i.e. an image) works fine to start a download. When I tested error situations like using FileDownloader with a FileResource and then removing the file before starting the download, the resulting exception is not shown in my ErrorHandler as it never gets called. Debugging the problem a bit, I found that the DefaultErrorHandler gets called instead.

The API method FileResource.getStream() which is called when the download starts says

Note that this method is called while the session is locked to prevent race conditions but the methods in the returned DownloadStream are assumed to be unrelated to the VaadinSession and are called without holding session locks (to prevent locking the session during long file downloads).

I suppose this to be the cause of the problem, but do not know how to prevent it.
Has anyone encountered the problem before?

Hi, the VaadinSession error handler is used in that case (called by VaadinService.handleExceptionDuringRequest), so if you simply call VaadinSession.setErrorHandler instead of the UI one, the FileDownloader should invoke your custom handler.

Incidentally, in Vaadin 7.2 this is
changed
to work in the usual manner so that the connector and its parents in the hierarchy are recursively searched, making your code work as-is in 7.2 :slight_smile:

Hello Johannes,

thanks for the quick reply.

I tried your first proposal with

    UI.getCurrent().getSession().setErrorHandler(myErrorHandler);

or
VaadinSession.getCurrent().setErrorHandler(myErrorHandler);

and my custom ErrorHandler gets called.

Unfortunately, a NullPointerException occurs in

at com.vaadin.ui.Notification.show(Notification.java:357)

which is called from my code with
new Notification(“Error”, message, Notification.Type.ERROR_MESSAGE).show(Page.getCurrent());

and Page.getCurrent() returns null as

public static Page getCurrent() {
    UI currentUI = UI.getCurrent();
    if (currentUI == null) {
        return null;
    }

UI.getCurrent() returns null. Is this due to the fact, that the FileDownloader is a background task?

Incidentally, in Vaadin 7.2 this is changed to work in the usual manner so that the connector and its parents in the >hierarchy are recursively searched, making your code work as-is in 7.2 :slight_smile:

Brilliant, I’ll give this a try with Vaadin 7.2. Setting the ErrorHandler just once is much more elegant :wink:

Oh, indeed. This is an unfortunate side effect of the fact that exceptions leak all the way to VaadinService. In Vaadin 7.2 the relevant threadlocals are set, but do note that this is a separate download request initiated by the browser. Thus any UI changes, like showing a notification, cannot be sent to the client side until the user causes a server trip, unless polling or server push is used.

Note that even in 7.1, you can simply set the session error handler
instead
of the UI handler; if the UI doesn’t have one, the session handler will be used. That is, of course, unless you actually need each UI to have a separate handler.

Thanks again, Johannes, for both tips.

Looks like I need to give my custom error handler some more thoughts :wink:
But I can at least log the error.