GithubHelp home page GithubHelp logo

webcloud / react-error-guard Goto Github PK

View Code? Open in Web Editor NEW
18.0 2.0 0.0 352 KB

⚛️An overlay for displaying stack frames based on create-react-app/packages/react-error-overlay

Home Page: https://www.npmjs.com/package/react-error-guard

License: MIT License

JavaScript 100.00%
react error-handling error-monitoring error-reporting error-page development-utils exception-handling exception-reporting

react-error-guard's Introduction

⚛️ React Error Guard Component ⚛️

Setup your consuming project

Firs install the package with yarn add react-error-guard (or npm i --save). Then you can consume the component like bellow.

const MyReactContainer = ({ children }) => {
  let ErrorBoundaryContainer = null;

  if (process.env.NODE_ENV !== 'production') {
    ErrorBoundaryContainer = require('react-error-guard').DeveloperErrorBoundary;
  } else {
    ErrorBoundaryContainer = require('react-error-guard').ProductionErrorBoundary;
  }

  return <ErrorBoundaryContainer>{children}</ErrorBoundaryContainer>;
}

ℹ️ Notice: This component right now is only compatible with React 16. But there are plans to support React 15 but much more limited version, unstable_handleError.

<DeveloperErrorBoundary />

It is meant to be used as a drop-in helper on development environments. It is a declarative component, abstracted from the react-error-overlay. The error screen from the development server from create-react-app.

DeveloperErrorBoundary preview

DeveloperErrorBoundary example

<ProductionErrorBoundary />

It is meant to be used on production environments, as a helper to provide a fallback error screen and also automated reports to your preferred tool via the dispatchErrorReporting prop.

Customizing <ProductionErrorBoundary />

ProductionErrorBoundary is highly customisable through its props.

Default ProductionErrorBoundary preview (with no dispatchErrorReporting provided)

<ProductionErrorBoundary>
  {children}
</ProductionErrorBoundary>

The code above will produce the following default error message component render:

no dispatchErrorReporting preview

Default ProductionErrorBoundary preview (with dispatchErrorReporting provided)

<ProductionErrorBoundary dispatchErrorReporting={dispatchErrorReporting}>
  {children}
</ProductionErrorBoundary>

The code above will produce the following default error message component render:

no dispatchErrorReporting preview

You can even replace the default error message by passing errorMessageComponent prop.

const ErrorMessageComponent = (
  <div className="error-message">
    <h1>🚨 Whoopsie, we crashed 🚨</h1>
    <p>Don't worry, an automated error report has been sent to our team of cats to handle this.</p>
  </div>
);

const IconClose = <span></span>;

const dispatchErrorReporting = ({ error, info }) => {
  Raven.captureException(error, {
    extra: {
      ...info, reactVersion: React.version
    },
    tags: { version, reactVersion: React.version }
  });
}

const MyReactContainer = ({ children }) => (
  <ProductionErrorBoundary
    errorMessageComponent={ErrorMessageComponent}
    closeIcon={<IconClose />}
    dispatchErrorReporting={dispatchErrorReporting}
  >
    {children}
  </ProductionErrorBoundary>
)

▶️ The example above uses raven-js but any reporting tool can be used.

🌟 This project started, and still is, an abstraction taken from the amazing works of the create-react-app team.

react-error-guard's People

Contributors

webcloud avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

react-error-guard's Issues

Outdated readme or wrong export?

Library exports only a production boundary as default

export {default as ProductionErrorBoundary} from './ProductionErrorBoundary';

But README states that we should do the following to get development version which obviously cannot work.

ErrorBoundaryContainer = require('react-error-guard').DeveloperErrorBoundary;

I had to do ugly variant, but would be nice to correct it :)

const DeveloperErrorBoundary = require('react-error-guard/lib/DeveloperErrorBoundary').default

Error boundaries should implement getDerivedStateFromError()

This a new warning coming from freshly released React 16.6.

Warning: ErrorBoundaryComponent: Error boundaries should implement getDerivedStateFromError(). In that method, return a state update to display an error message or fallback UI.

I am not sure how it should work just yet, but I suppose it should be adapted to this.

Doesn't work, I got blank page

Hi,

I tired to replace react-error-guard with default react ErrorBoundary, but it doesn't show anything when error happens. (ErrorBoundary works as expected btw)

I also got this in browser console:

React will try to recreate this component tree from scratch using the error boundary you provided, ErrorBoundaryComponent.

I use:

  • React v16.5.0
  • Webpack v4.18.0

Would you help me on this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.