2

我正在尝试将Airbrake-js集成到我的开发设置中以报告 JavaScript 错误。我的应用是一个使用 webpack 的 React 应用。

我已经在网上搜索了几个小时来弄清楚应该如何设置。我无法在他们的文档中指出他们的起始代码在哪里:

var airbrakeJs = require('airbrake-js');
var airbrake = new airbrakeJs({projectId: 1, projectKey: 'abc'});

实际应该定位。我的想法是,我希望将我现有的应用程序包装在它们的 wrap 函数中(为简洁起见,仅显示我的 app.js 文件的一部分):

var airbrakeJs = require('airbrake-js'),
    React  = require('react'),
    ReactDOM = require('react-dom'),
    Relay = require('react-relay'),
    App = require('./components/app'),
    Router = require ('react-router'),
    Route = require Router.Route, 
    createBrowserHistory = require('history/lib/createBrowserHistory');

var startApp = function() {
  // This will throw if the document has no head tag.
  // ****What does this exactly do?**** 
  document.head.insertBefore(document.createElement("style"));
}
startApp = airbrake.wrap(myApp);

// Any exceptions thrown in startApp will be reported to Airbrake.
startApp();

ReactDOM.render((
  <Router history={createBrowserHistory()} createElement={ReactRouterRelay.createElement}>
    <Route component={App} queries={AppQueries} {...renderProps}>
      <Route path="/somewhere" component={Somewhere} queries={SomewhereQueries} />
    </Route>
  </Router>
), document.getElementById('academy-body'));

所以,我将它添加到我的入口点 app.js 文件(我也处理我的路由器逻辑),但它没有在那里被调用,所以我一定做错了什么。我也不完全知道第二行(在第一条评论下方)究竟做了什么。

任何人都可以解释并帮助正确设置 Airbrake 吗?Airbrake-js 甚至可以与 React 应用程序一起使用吗?

非常感谢您在正确方向上的任何帮助!

4

1 回答 1

1

您应该使用

window.airbrake = new airbrakeJs({projectId: 1, projectKey: 'abc'});
// replace projectId and projectKey with your project values

因此,首先您可以尝试将其添加到现有代码中。请注意,我们使用window全局范围从任何子范围访问 Airbrake 的实例。通常这不是一个好的做法,但对于 Airbreake 的目的来说看起来足够公平。

这条线

 document.head.insertBefore(document.createElement("style"));

这只是文档中使用的一个示例,可能会引发错误。您可以将其从您的代码中删除。

这是为了将 Airbrake 包裹在特定的功能上。

startApp = airbrake.wrap(myApp);

// Any exceptions thrown in startApp will be reported to Airbrake.
startApp();

您可以将其从代码中删除。

如果 Airbrake 确实在捕获错误,您可以使用它来调试:

// Remove it for production or use an enviroment conditional

airbrake.addReporter(function(notice) {
  console.log(notice);
});
于 2016-07-19T00:09:20.610 回答