Skip to content

Commit bfea2b4

Browse files
committed
feat(slim/blocks): add ErrorHandler block
1 parent 24f816b commit bfea2b4

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from './ErrorHandler'

0 commit comments

Comments
 (0)