React Error & Exception Tracking

Typical installation time: 5 minutes

Hi there! You've found Honeybadger's guide to React error and exception tracking. Once installed, Honeybadger will automatically report errors from your React application.

Installation

Add @honeybadger-io/react as a dependency.

# npm npm add @honeybadger-io/js @honeybadger-io/react --save # yarn yarn add @honeybadger-io/js @honeybadger-io/react

In your main.js:

javascript
import React from 'react' import ReactDOM from 'react-dom' import './index.css' import App from './App' import { Honeybadger, HoneybadgerErrorBoundary } from '@honeybadger-io/react' const config = { apiKey: 'Your project API key', environment: 'production', revision: 'git SHA/project version' } const honeybadger = Honeybadger.configure(config) ReactDOM.render(<HoneybadgerErrorBoundary honeybadger={honeybadger}><App /></HoneybadgerErrorBoundary>, document.getElementById('root'))


Note: Errors that happen in development and test environments are not reported by default. To always report errors or to change the defaults, see Environments and Versions.


HoneyBadgerErrorBoundary Props

honeybadger
The Honeybadger config object.
children
Your root <App /> component.
ErrorComponent (optional — default: "DefaultErrorComponent")
The component that will be rendered in ErrorBoundary children's place when an error is thrown during React rendering. The default value for this prop is the DefaultErrorComponent.

DefaultErrorComponent

jsx
class DefaultErrorComponent extends Component { render() { return ( <div className='error'> <div>An Error Occurred</div> <div>{this.error}</div> <div>{this.info}</div> </div> ) } }

Reporting errors

Using the example configuration above, you'll install @honeybadger-io/react as React's error handler.

Additionally, by default, an error handler for all JavaScript errors will be attached to the window.onerror handler for JavaScript errors that may originate from React components or other JavaScript on the page.

Because React doesn't intercept all errors that may occur within a React component, errors that bubble up to the window.onerror handler may be missing some React component contextual information, but the stack trace will be available.

If, for some reason, you do not wish to install Honeybadger's error handler on the global window.onerror handler, you may add { enableUncaught: false } to the configuration you're passing to Honeybadger.configure.

You may also manually report errors by directly invoking the honeybadger.js API.

javascript
honeybadger.notify(error)

See the full documentation for more options.

Note: React handles exceptions slightly differently in development mode than in production. As a result, in development mode only, the error boundary component you create will capture the error and add React context, but React will essentially re-throw the error and it will bubble up to the window.onerror handler (without the React context data). This behavior will not occur when your React application is built for production.

Identifying users

Honeybadger can track which users have encountered each error. To identify the current user in error reports, add a user identifier and/or email address with honeybadger.setContext:

javascript
honeybadger.setContext({ user_id: 123, user_email: 'user@example.com' });

Sending additional context

Sometimes additional application state may be helpful for diagnosing errors. You can arbitrarily specify additional key/value pairs when you invoke setContext.

javascript
honeybadger.setContext({ active_organization: 55, custom_configuration: false });

Clearing context

If your user logs out or if your context changes during the React component lifetime, you can set new values as appropriate by invoking setContext again.

Additionally, if needed, you can clear the context by invoking clear:

javascript
// Set the context to {} honeybadger.clear();

Advanced usage

@honeybadger-io/react is built on honeybadger.js.

See the Honeybadger JavaScript integration documentation for additional customization options.

Tracking deploys

As with vanilla JavaScript applications, you can notify Honeybadger when you've deployed a new build. Honeybadger will associate an error report with a specific revision number (matching the 'revision' field in the configuration passed to Honeybadger.configure).

Here's a simple curl script to record a deployment:

sh
HONEYBADGER_ENV="production" \ HONEYBADGER_REVISION="git SHA/project version" \ HONEYBADGER_API_KEY="Your project API key" \ curl -g "https://api.honeybadger.io/v1/deploys?deploy[environment]=$HONEYBADGER_ENV&deploy[local_username]=$USER&deploy[revision]=$HONEYBADGER_REVISION&api_key=$HONEYBADGER_API_KEY"

Be sure that the same revision is also configured in the @honeybadger-io/react library. Read more about deploy tracking in the API docs.

Tracking Deploys from Netlify

If you are deploying your site to Netlify, you can notify Honeybadger of deployments via Netlify's webhooks. Go to the Deploy notifications section of the Build & deploy tab for your site settings, and choose to add an Outgoing webhook notification. Choose Deploy succeeded as the event to listen for, and use this format for your URL:

https://api.honeybadger.io/v1/deploys/netlify?api_key=YOUR_HONEYBADGER_API_KEY_HERE

The environment that will be reported to Honeybadger defaults to the Netlify environment that was deployed, but you can override that with &environment=CUSTOM_ENV in the webhook URL, if you like.

Source map support

Honeybadger can automatically un-minify your code if you provide a source map along with your minified JavaScript files. See our Source Map Guide for details.

Collect User Feedback

When an error occurs, a form can be shown to gather feedback from your users. Honeybadger can automatically show the form by setting the showUserFeedbackFormOnError prop to true:

javascript
<HoneybadgerErrorBoundary honeybadger={honeybadger} showUserFeedbackFormOnError={true}> <App /> </HoneybadgerErrorBoundary>

Read more about this feature here.

Sample application

A minimal implementation is included in the example folder in the @honeybadger-io/react repository.

To run it from the command line, enter the following commands in your shell:

bash
cd example yarn install REACT_APP_HONEYBADGER_API_KEY=yourkey yarn start

Observe the command-line output to determine the appropriate URL to connect to in your browser (usually http://localhost:3000/).