使用Sentry的Raven JS收集所有HTTP错误

时间:2016-03-01 12:18:03

标签: javascript logging sentry raven

我已经设置了一个Sentry应用程序来收集客户端可能发生的HTTP / JS错误。但是,似乎当我尝试发出大约400个HTTP请求时,Sentry无法相应地捕获请求。

它是Sentry的默认行为,还是我的代码中缺少某些内容(如下)?

<!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>

感谢您的反馈

3 个答案:

答案 0 :(得分:2)

您可以使用ajaxError处理程序(https://api.jquery.com/ajaxError/):

$( document ).ajaxError(function( event, request, settings ) {
  Raven.captureException(new Error(JSON.stringify(request)));
});

答案 1 :(得分:2)

Sentry docs on function wrapping将是这方面最规范的来源,但有效地告诉你做鲍勃所说的。 :)

答案 2 :(得分:1)

我发现这可以最好地将所有可用信息组合到其他数据字段中:

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