title: StreamResourceWriter error handling via ErrorHandler interface
body: <p>I have a FileCreator class that implements StreamResourceWriter interface and MainErrorHandler class that implements ErrorHandler. I'm using the <code>MainErrorHandler</code> class as a centralized Exception handler in my project which mostly logs the exception and shows a notification to the user. The problem is that StreamResourceWriter.accept() method runs in a non UI thread and when an Exception is thrown it is directed to the ErrorHandler which then fails to show a notification due to "<strong>IllegalStateException</strong>: UI instance is not available". <strong>Is there a way to show a notification window to the user from MainErrorHandler when FileCreator throws an error in <code>accept()</code> method?</strong></p>
  
  <p>Below FileCreator snippet.</p>
  
  <pre><code>public class FileCreator implements StreamResourceWriter {
      @Override
      public void accept(OutputStream stream, VaadinSession session) throws IOException {
          // Run in a non ui thread.
          // Writes to OutputStream but an Exception might be thrown during this process
      }
  }
  </code></pre>
  
  <p>Below MainErrorHandler snippet.</p>
  
  <pre><code>/**
   * Centralized error handler
   */
  public class MainErrorHandler implements ErrorHandler {
      private static final Logger log = LoggerFactory.getLogger(MainErrorHandler.class);
      @Override
      public void error(ErrorEvent event) {
          log.error("Error occurred", event.getThrowable());
          //Cannot show a notification if ErrorEvent came from FileCreator.
          //Will get an IllegalStateException: UI instance is not available.
          Notification.show("Error occurred");
          //Tried UI.getCurrent but it returns null if ErrorEvent came from FileCreator.
          UI.getCurrent();
      }
  }
  </code></pre>
  
  <p>Using Vaadin 13.0.1.</p>
  
  <p>Edit</p>
  
  <p>One way to solve this issue is to pass UI reference to FileCreator directly. Below an example.</p>
  
  <pre><code>public class FileCreator implements StreamResourceWriter {
      private UI ui;
      //Pass UI reference directly
      public FileCreator(UI ui){
         this.ui = ui;                                                        
      }
      @Override
      public void accept(OutputStream stream, VaadinSession session) throws IOException {
         try{
          // Run in a non ui thread.
          // Writes to OutputStream but an Exception might be thrown during this process
         }catch(Exception e){
             //I don't like this since have to catch all exceptions and have to call ErrorHandeler directly with a UI reference. Also what if somewhere in code ErrorHandler is changed and is not of type MainErrorHandler.
             ((MainErrorHandler)VaadinSession.getCurrent().getErrorHandler()).error(e, ui);
         }
      }
  }
  </code></pre>
  
  <p>As I said in comments I really don't like this approach either since I am forced to catch all Exceptions, have to cast ErrorHandler to MainErrorHandler and calling it directly.</p>