Gatsby: How to integrate gatsby.js with Bugsnag?

Created on 23 May 2019  路  6Comments  路  Source: gatsbyjs/gatsby

Summary

I was wondering if anyone had some experience using Bugsnag (bugsnag.com) with Gatsby?
As I'm not too sure how to do it.

Thank you in advance for your help.

stale? question or discussion

Most helpful comment

Thank you @lannonbr for your quick reply. It works.

Here is what I ended up doing for future reference:

_gatsby-browser.js_
```import React from 'react';
import { Provider } from 'react-redux';
import bugsnag from '@bugsnag/js';
import bugsnagReact from '@bugsnag/plugin-react';
import createStore from './src/state/createStore';

const bugsnagClient = bugsnag('BUGSNAG_ID');
bugsnagClient.use(bugsnagReact, React);
const ErrorBoundary = bugsnagClient.getPlugin('react');

const store = createStore();

const wrapRootElement = ({ element }) => (

{element}


);

export default wrapRootElement;```

All 6 comments

@rgesulfo It looks like they have support for React as described here: https://docs.bugsnag.com/platforms/javascript/react/

That said, they describe that you can call ReactDOM.render to wrap your app with a <ErrorBoundary> component. In the Gatsby world, we reccomend if you want to wrap the root element, use the wrapRootElement api described in the Gatsby Browser APIs

@lannonbr @rgesulfo exactly! You'll want to use the APIs that Benjamin noted.

What would be _super cool_ is you first get this working with your own application, and once done, this can then be extracted into a standalone plugin so that anyone in the Gatsby ecosystem can get Bugsnag functionality working in their application super-easily!

The idea here is that you'll use gatsby-browser.js to import the necessary dependencies, and then wrap your root element if you want the additional functionality specific to React (which you probably do).

Hope this helps--going to close as answered, but please reply or re-open if we can help further. Thanks for using Gatsby!

Thank you @lannonbr for your quick reply. It works.

Here is what I ended up doing for future reference:

_gatsby-browser.js_
```import React from 'react';
import { Provider } from 'react-redux';
import bugsnag from '@bugsnag/js';
import bugsnagReact from '@bugsnag/plugin-react';
import createStore from './src/state/createStore';

const bugsnagClient = bugsnag('BUGSNAG_ID');
bugsnagClient.use(bugsnagReact, React);
const ErrorBoundary = bugsnagClient.getPlugin('react');

const store = createStore();

const wrapRootElement = ({ element }) => (

{element}


);

export default wrapRootElement;```

I have attempted to write such a plugin - but it's somewhat tricky in that if the application's gatsby-browser implements wrapRootElement it will wrap the plugin's implementation

<!-- app's wrapRoot -->
<Provider store={store}>
    <!-- plugin's wrapRoot -->
    <ErrorBoundary>
      {element}
    </ErrorBoundary>
  </Provider>

I wonder if there's an opportunity to think about a hook especially for placing an ErrorBoundary at the top of the stack.

In the example above I think that errors that occur in the Provider would not be caught by the ErrorBoundary. Or am I missing a detail?

Hiya!

This issue has gone quiet. Spooky quiet. 馃懟

We get a lot of issues, so we currently close issues after 30 days of inactivity. It鈥檚 been at least 20 days since the last update here.

If we missed this issue or if you want to keep it open, please reply here. You can also add the label "not stale" to keep this issue open!

As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out gatsby.dev/contribute for more information about opening PRs, triaging issues, and contributing!

Thanks for being a part of the Gatsby community! 馃挭馃挏

Hey again!

It鈥檚 been 30 days since anything happened on this issue, so our friendly neighborhood robot (that鈥檚 me!) is going to close it.

Please keep in mind that I鈥檓 only a robot, so if I鈥檝e closed this issue in error, I鈥檓 HUMAN_EMOTION_SORRY. Please feel free to reopen this issue or create a new one if you need anything else.

As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out gatsby.dev/contribute for more information about opening PRs, triaging issues, and contributing!

Thanks again for being part of the Gatsby community!

Was this page helpful?
0 / 5 - 0 ratings