Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup Airbrake for your React application #7

Open
AReid987 opened this issue May 26, 2024 · 0 comments
Open

Setup Airbrake for your React application #7

AReid987 opened this issue May 26, 2024 · 0 comments
Labels

Comments

@AReid987
Copy link
Owner

Installation

Add the library

npm install @airbrake/browser

Configuration

To report errors from a React app, you'll need to set up and use an ErrorBoundary component and initialize a Notifier with your projectId and projectKey.

(You can find your project ID and API key in your project's settings)

import React, { Component } from 'react';
import { Notifier } from '@airbrake/browser';

class ErrorBoundary extends React.Component {
  constructor(props) {
    super(props);
    this.state = { hasError: false };
    this.airbrake = new Notifier({
      projectId: <Your project ID>,
      projectKey: '<Your project API Key>'
    });
  }

  componentDidCatch(error, info) {
    // Display fallback UI
    this.setState({ hasError: true });
    // Send error to Airbrake
    this.airbrake.notify({
      error: error,
      params: {info: info}
    });
  }

  render() {
    if (this.state.hasError) {
      // You can render any custom fallback UI
      return <h1>Something went wrong.</h1>;
    }
    return this.props.children;
  }
}

export default ErrorBoundary;

Then you can use it as a regular component:

<ErrorBoundary>
  <MyWidget />
</ErrorBoundary>

To test that Airbrake has been installed correctly in your project, start up
your JS project locally. Then visit it in your browser (eg:
http://localhost:1080), open the JS console, and paste in the following to
trigger a test error:

window.onerror("TestError: This is a test", "path/to/file.js", 123);

This should send a test error to your Airbrake project.

Full documentation

Visit our official GitHub repo for more info on alternative configurations and advanced options.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant