File tree Expand file tree Collapse file tree 2 files changed +36
-0
lines changed
recipes/slim/snippets/sources/components/blocks/ErrorHandler Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Original file line number Diff line number Diff line change 1+ import React , { Component } from 'react'
2+
3+ export default class ErrorBoundary extends Component {
4+ constructor ( props ) {
5+ super ( props )
6+ this . state = { error : null , errorInfo : null }
7+ }
8+
9+ componentDidCatch ( error , errorInfo ) {
10+ // Catch errors in any components below and re-render with error message
11+ this . setState ( {
12+ error : error ,
13+ errorInfo : errorInfo ,
14+ } )
15+ // You can also log error messages to an error reporting service here
16+ }
17+
18+ render ( ) {
19+ if ( this . state . errorInfo ) {
20+ // Error path
21+ return (
22+ < div >
23+ < h2 > Something went wrong.</ h2 >
24+ < details style = { { whiteSpace : 'pre-wrap' } } >
25+ { this . state . error && this . state . error . toString ( ) }
26+ < br />
27+ { this . state . errorInfo . componentStack }
28+ </ details >
29+ </ div >
30+ )
31+ }
32+ // Normally, just render children
33+ return this . props . children
34+ }
35+ }
Original file line number Diff line number Diff line change 1+ export { default } from './ErrorHandler'
You can’t perform that action at this time.
0 commit comments