title: How to override window.onerror with ngZone in an angular app?
body: <p>Within my angular 14 app I am integrating a 3rd-party script, called <code>JS9</code>, by including scripts within <code>angular.json</code>. When angular runs the compiled code, it executes these 3rd-party scripts and thereby provides a global <code>JS9</code> object, which has various methods attached to it. When I run one of the methods (e.g. <code>JS9.Load(...)</code>), it will sometimes throw an error, and I need to be able to handle such errors gracefully within my angular app.</p>
  <p>This 3rd-party <code>JS9</code> API does NOT provide a working <code>onError</code> callback. So the only way that I can think to catch any such error (without editing this 3rd-party spaghetti code), is to try and modify my <code>window.onerror</code> function, by putting something like the following into my angular global space:</p>
  <pre><code>window.onerror = function (message, file, lineNumber) {
    console.error('Message within monkey-patched onerror:', message);
    return true;
  };
  </code></pre>
  <p>If I throw an error in my angular code, this modified <code>window.onerror</code> function catches it.</p>
  <p>However, this modified <code>window.onerror</code> function does not catch the errors thrown by <code>JS9</code>. Instead, I get a call stack like the following:</p>
  <pre><code>ERROR Error: ERROR from astroem/cfitsio: datatype conversion overflow
      at JS9.error (js9.js:24130:1)
      at Module.error (astroemw.js:5216:23)
      at Module.errchk (astroemw.js:5211:20)
      at Module.getFITSImage (astroemw.js:5599:19)
      at fileReader.onload (astroemw.js:5773:29)
      at _ZoneDelegate.invoke (zone.js:372:26)
      at Object.onInvoke (core.mjs:26503:33)
      at _ZoneDelegate.invoke (zone.js:371:52)
      at Zone.runGuarded (zone.js:144:47)
      at FileReader.&lt;anonymous&gt; (zone.js:128:29)
  </code></pre>
  <p>So what is going on here? I assume this has something to do with <code>ngZone</code>, but I don't know how to work with <code>ngZone</code> to catch errors thrown by scripts that are incorporated into the angular app via <code>angular.json</code>. Thanks in advance!</p>