title: Catch Google Maps API errors when using requirejs async plugin
body: <p>I have a JS app in deployment that depends upon Google Maps.  I load the Google Maps SDK for JS using the async plugin for RequireJS...</p>
  
  <pre><code>mapsKey = '&amp;key=' + MY_API_KEY;
  mapsURL = 'https://maps.googleapis.com/maps/api/js?libraries=places,geometry&amp;v=3' + mapsKey;
  define('GoogleMaps', ['async!' + mapsURL], function() {
      return window.google.maps;
  });
  </code></pre>
  
  <p>I fetch MY_API_KEY from our server as I'd rather not have it in the app code.  If our server is temporarily unavailable and the key value cannot be fetched, or for some reason Google rejects the API Key, there is a console error...</p>
  
  <pre><code>"Google Maps API error: Google Maps API error: InvalidKeyMapError https://developers.google.com/maps/documentation/javascript/error-messages#invalid-key-map-error"
  </code></pre>
  
  <p>and worse, an ugly popup in the app...</p>
  
  <p><a href="https://i.stack.imgur.com/Shdkk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Shdkk.png" alt="enter image description here"></a></p>
  
  <p>I have a valid API Key setup with Google and things work fine.  But if there is a loading error, I'd like to retry without an API Key.  Google does emit a warning in the JS console about the missing key, but at least for now it still works as a last resort.</p>
  
  <p>QUESTION: How does one catch Google Maps API errors?</p>
  
  <p>The error is thrown from within the Google Maps library during the loading process.  This is not a timeout, but an invalid key parameter error.  Checking the resulting window.google.maps instance is not an option, as the ugly popup message is already displayed.  I do not see a way to trap the error and prevent the popup.  I'd like to provide some defensive code to retry without an API Key as a last resort.</p>