4

I have setup a Sentry application to collect HTTP / JS errors that can occur on the client side. However, it seems that when I try to make some 400 HTTP requests, Sentry failed to capture the request accordingly.

Is it the default behavior of Sentry, or if something missing in my code (below)?

<!DOCTYPE html>
<html>
    <head>
            <title>Hi there</title>
            <script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
            <script src="https://cdn.ravenjs.com/2.1.1/raven.min.js"></script>
            <script>Raven.config('http://xxx@xxx.xxxxx.com/4').install();</script>
    </head>
    <body>
        Hello the world :-)
        <script type="text/javascript">
            $.get("http://somehttp400url.com/");
        </script>
    </body>
</html>

Thanks for your feedback

Hoang HUA
  • 325
  • 4
  • 7

3 Answers3

3

You can use ajaxError handler ( https://api.jquery.com/ajaxError/ ):

$( document ).ajaxError(function( event, request, settings ) {
  Raven.captureException(new Error(JSON.stringify(request)));
});
Bob Sponge
  • 4,708
  • 1
  • 23
  • 25
  • 1
    Great! it works. Thanks. Btw, if we have a script or style tag pointing to an invalid file (which will return 404), is it possible to be collected by Sentry as well? – Hoang HUA Mar 01 '16 at 16:15
  • No, there's actually no way to get that information automatically. You could try to do it this way: – ehfeng Mar 02 '16 at 03:52
  • 1
    @HoangHUA there is a better way than explicitly checking each lib. You can use onerror: `` and: `function trackFailure(msg) { Raven.captureException(msg) }` – Jenian Dec 08 '16 at 13:23
2

Sentry docs on function wrapping will be the most canonical source on this, but effectively tell you to do what Bob says. :)

ehfeng
  • 3,807
  • 4
  • 33
  • 42
1

I found this to work best combining all available information into additional data field:

$(document).ajaxError(function(event, jqXHR, ajaxSettings, thrownError) {
  var new_obj= Object.assign({},jqXHR, ajaxSettings);
  Raven.captureMessage(thrownError || jqXHR.statusText, {
        extra: new_obj
  });
});
mojovski
  • 581
  • 7
  • 21