title: Catch window.open() error
body: <p>I open a file download from a remote API on my webpage via <code>window.open()</code>. The API (a Flask server) has error handling and returns the error message if there's an internal server error, like this:</p>
  
  <pre><code>@app.errorhandler(502) //all other errors are handled the same way, including 500, etc.
  @crossdomain(origin='*')
  def bad_gateway_error(error):
      return "Bad Gateway Error - Please make sure you're using a properly formatted file! Details: " + str(error), 200
  </code></pre>
  
  <p>I want to display this error on my site instead of redirecting to the error page. I'm trying to catch it via:</p>
  
  <pre><code>try {
      window.open("https://API/receivedoc?timestamp="+timestamp,"_self")
  } catch(e) {
      filerootdiv.querySelector('.output').innerHTML = String(e);
  }
  </code></pre>
  
  <p>This however does nothing (tested in Chrome). How could I catch the error when I'm using <code>window.open</code>? I guess it might be because in the error handling I return a 200 message so that the string I return actually gets returned instead of just crashing the server (this needs to stay this way as it's working just fine with all the other errors when I'm not trying to return a file). The issue is that I can't tell if the API request would return a file or a string before doing a <code>window.open()</code>.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I've tried implementing:</p>
  
  <pre><code>let new_window = window.open("https://flaskmin.run.aws-usw02-pr.ice.predix.io/receivedoc?timestamp="+timestamp,"_self")
  newWindow.onerror = function() {
    filerootdiv.querySelector('.output').innerHTML = "Error!";
  </code></pre>
  
  <p>However this still only opens a new window with the error. I guess it's because of the error handling on the server side (I cannot change this). Can I somehow probe the content of <code>new_window</code> before redirecting to it, and just not open it if it's just a string containing the word 'error'?</p>