exceptionless / Exceptionless.JavaScript

Exceptionless JavaScript client
http://exceptionless.com
Apache License 2.0
59 stars 24 forks source link

Unhandled exceptions aren't logged automatically #54

Closed PhilippSoehnlein closed 8 years ago

PhilippSoehnlein commented 8 years ago

The Readme claims:

Once configured, Exceptionless.js will automatically submit any unhandled exceptions that happen in your application to the Exceptionless server.

For me it looks like this isn't the case. Here's a reduced test case in which I would expect to get three exceptions logged to the server, which doesn't happen:

<!DOCTYPE html>

<html>
<head>
    <title>Exceptionless test</title>
    <script src="https://raw.githubusercontent.com/exceptionless/Exceptionless.JavaScript/master/dist/exceptionless.js"></script>
    <script>
        var exceptionlessClient = new exceptionless.ExceptionlessClient( {
            apiKey: 'YOUR_API_KEY',
            serverUrl: 'YOUR_SERVER',
        } );
  </script>
</head>

<body>
    <button type="button">Throw exception, plz.</button>

    <script>
        // This isn't reported to the Exceptionless server:
        window.setTimeout(
            function() { throw new Error( 'Test error in setTimeout handler' ); },
            1000
        );

        // This isn't either:
        document.getElementsByTagName( 'button' )[0].addEventListener(
            'click',
            function() { throw new Error( 'Test error in click handler' ); },
            false
        );

        // No report, again…
        (function xhrExceptionTest() {
            var myRequest = new XMLHttpRequest();
            myRequest.open( 'GET', 'http://httpstat.us/500', true );
            myRequest.onreadystatechange = function() {
                if ( myRequest.readyState === myRequest.DONE && myRequest.status !== 200 ) {
                    throw new Error( 'Test error in XHR handler' );
                }
            };
            myRequest.send( null );
        }());

        // This works:
        // exceptionlessClient.submitException( new Error( 'submitException' ) );
    </script>
</body>
</html>

I tested with Firefox 46, Chrome 50 and Safari 9.1 on Mac OS 10.11.4.

niemyjski commented 8 years ago

Thanks for the sample, I'll take a look into this.

niemyjski commented 8 years ago

Hey, I'm really sorry I haven't taken a look at this sooner. I'm currently looking into this.

niemyjski commented 8 years ago

This behavior is by design. Only the default client instance wires up to the environment specific error handlers as shown here (https://github.com/exceptionless/Exceptionless.JavaScript/blob/master/src/exceptionless.ts) for browsers. When you new up your own instance of the exceptionless client, you are responsible for wiring up to these handlers. The question should be: do we add a register or startup method that we call so you can hook up multiple clients to these error handlers.

To get them to log just set the api key on the default instance exceptionless.ExceptionlessClient.default or part of the query string exceptionless.js?apiKey=YOUR_API_KEY. Once you do this everything will be picked up:

screen shot 2016-07-08 at 2 06 40 pm

Please let me know if you need any help or have any questions.