5

I tried to implement error boundary by https://reactjs.org/docs/error-boundaries.html, but it catches only rendering errors.

It is possible to implement something like global try/catch on react and catch errors also on async processes and handlers?

Something like:

try {
    ReactDOM.render(<App />, document.getElementById("root"));
} catch (error) {
    console.log("CAUGHT AN ERROR!!!!!!!")
    console.log(error);
    ReactDOM.render(<ErrorComponent />, document.getElementById("root"));

}
Edgaras Karka
  • 7,400
  • 15
  • 61
  • 115
  • Error Boundaries catch, not only issues in render, but also lifecycle-related errors. In other words, it catches most error your component could throw, except async operations and event handlers – Chris May 22 '19 at 08:39
  • You can override `console.error` and try handling in it. – Rajesh May 22 '19 at 08:42
  • @Rajesh maybe can share the link to example? I tried https://ourcodeworld.com/articles/read/104/how-to-override-the-console-methods-in-javascript but it looks like does not work for me – Edgaras Karka May 22 '19 at 09:44
  • @EdgarasKarka Long back I made this as a [test fiddle](https://jsfiddle.net/RajeshDixit/2pwrth00/). You can take it as a reference. Idea is to capture all exceptions, no matter the origin. [Stack post](https://stackoverflow.com/questions/1215392/how-to-quickly-and-conveniently-disable-all-console-log-statements-in-my-code) may also help – Rajesh May 22 '19 at 09:58

2 Answers2

6

React 16 has a new feature called error boundaries. You can search more about this. Your problem is solved by error boundary like this : Ex :

class ErrorBoundary extends React.Component {
  constructor(props) {
    super(props);
    this.state = { hasError: false };
  }

  static getDerivedStateFromError(error) {
    // Update state so the next render will show the fallback UI.
    return { hasError: true };
  }

  componentDidCatch(error, info) {
    // You can also log the error to an error reporting service
    logErrorToMyService(error, info);
  }

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

    return this.props.children; 
  }
}

And you can wrap your App component like this. It will catch all error :

<ErrorBoundary>
  <App />
</ErrorBoundary>

You can read more about here. https://reactjs.org/docs/error-boundaries.html

Shubham Verma
  • 4,918
  • 1
  • 9
  • 22
2

One way is to override global console.error method.

 var console=(function(oldCons){
     return {
         log: function(text){
             oldCons.log(text);
         },
         info: function (text) {
             oldCons.info(text);
         },
         warn: function (text) {
             oldCons.warn(text);
         },
         error: function (text) {
             oldCons.error(text);
             ReactDOM.render(<div>ERROR</div>, document.getElementById('root'))
         }
     };
 }(window.console));

 // catch unhandled promises
 window.addEventListener("unhandledrejection", (event) => {
   console.error(event.reason);
 });
Edgaras Karka
  • 7,400
  • 15
  • 61
  • 115