1

Similarly to ask made in this question, I am trying to push all unhandled errors to our telemetry pipeline in order to provide enough information for engineers to debug the problems.

The unhandled exception handler was set up in following way:

window.onerror = (event: Event | string, source?: string, lineno?: number, colno?: number, error?: Error) => {
        const message: string = `src: ${source} line: ${lineno} col: ${colno}`;
        logError(error || new Error(event.toString()), message);
    };

This seems to be inline with documentation.

After telemetry started flowing I observed following:

  1. For majority of handler invocations error variable is null.
  2. lineno and colno are also either null or 0.

I am aware of CROSSORIGIN setting and it seems to be set up properly.

I wasn't able to find any strict rules that describe what info is provided during unhandled callback. I am also looking for feedbacks on the way we set up unhandled callback and ideas for potential improvements.

Klark
  • 8,162
  • 3
  • 37
  • 61
  • Does the server set the CORS headers as well? And are the values really `null` or just undefined (which `JSON.stringify` converts to `null`)? – user3840170 Apr 04 '21 at 19:28
  • 1
    Can you show what your `logError` function does or tell us how you determine that the error variable is null? I've tested your handler with a variety of weird errors, and it should work OK, except for not handling failed requests / other promises. – Marian Apr 04 '21 at 22:50
  • logError is pretty simple. It just checks if error is an instance of `Error` and extracts message and stack and pushes it to telemetry endpoint. – Klark Apr 05 '21 at 09:28
  • 3
    [Here's a plunk](https://plnkr.co/edit/JtDlM3nMuvP7el5Q?open=lib%2Fscript.js) with some tests. Even in cases where you have `throw null` it still works (because you do `error || new Error(...)`). Which leads me to believe that the issue might be somewhere at the ingestion point of this telemetry. As an aside, I've recently spent some time setting up Sentry for my org. It can be set-up in a way that it shows the typescript source lines in the stack traces instead of minified javascript code. – Marian Apr 05 '21 at 10:38
  • Have you checked on the client side if the `error` is `null`? – TheChetan Apr 11 '21 at 17:47

1 Answers1

-2
<script type="text/javascript">
window.onerror = function(msg, url, line, col, error) {

    const message: string = `src: ${source} line: ${lineno} col: ${colno}`;
    logError(error || new Error(event.toString()), message);

   // Send your telemetry info here

   // Many browsers show alerts dialogs when errors are founded, so return true to suppress it.
   return true;
};
</script>

When does the window.onerror fire?

The event is raised when either

  1. there is an uncaught exception
  2. a compile time error occurs

For example:

uncaught exceptions

  • throw "some messages"
  • call or reference to undefined;
  • cross_origin_iframe.contentWindow.document;, a security exception

compile error

<script>{</script>
<script>for(;)</script>
<script>"oops</script>
setTimeout("{", 10);, it will attempt to compile the first argument as a script

Browsers supporting window.onerror

  • Chrome 13+
  • Firefox 6.0+
  • Internet Explorer 5.5+
  • Opera 11.60+
  • Safari 5.1+