Skip to content

Commit

Permalink
Added ErrorBoundary, in case that improves error-handling
Browse files Browse the repository at this point in the history
  • Loading branch information
DuncanRitchie committed Sep 16, 2024
1 parent cbc3910 commit 3a46a26
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
34 changes: 34 additions & 0 deletions components/errorBoundary/errorBoundary.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Component } from 'react'

// Adapted from https://nextjs.org/docs/pages/building-your-application/configuring/error-handling
class ErrorBoundary extends Component {
constructor(props) {
super(props)
this.state = { hasError: false }
}
static getDerivedStateFromError(error) {
return { hasError: true }
}
componentDidCatch(error, errorInfo) {
console.log({ error, errorInfo })
}
render() {
if (!this.state.hasError) {
return this.props.children
}
return (
<p>
There was a glitch.{' '}
<button
type="button"
onClick={() => this.setState({ hasError: false })}
style={{ backgroundColor: '#7aefb3', padding: '0.5rem 0.75rem', marginInline: '0.5rem' }}
>
Try again?
</button>
</p>
)
}
}

export default ErrorBoundary
9 changes: 7 additions & 2 deletions pages/_app.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import DefaultHead from '../components/defaultHead/DefaultHead'
import ErrorBoundary from '../components/errorBoundary/errorBoundary'
import Footer from '../components/footer/Footer'
import styles from '../css/globals.css'

function App({ Component, pageProps }) {
return (
<>
<DefaultHead />
<Component {...pageProps} />
<Footer />
<ErrorBoundary>
<Component {...pageProps} />
</ErrorBoundary>
<ErrorBoundary>
<Footer />
</ErrorBoundary>
</>
)
}
Expand Down

0 comments on commit 3a46a26

Please sign in to comment.