Skip to content

Commit

Permalink
Add ErrorBoundary (#19)
Browse files Browse the repository at this point in the history
add ErrorBoundary
  • Loading branch information
julienbrg authored Jul 3, 2024
1 parent 5732a03 commit 263d594
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 8 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"next-seo": "^6.5.0",
"postcss": "8.4.23",
"react": "18.2.0",
"react-device-detect": "^2.2.3",
"react-dom": "18.2.0",
"typescript": "5.0.4"
}
Expand Down
18 changes: 18 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 56 additions & 0 deletions src/components/layout/ErrorBoundary.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import React, { ErrorInfo, ReactNode } from 'react'
import { mobileModel, mobileVendor } from 'react-device-detect'

interface Props {
children: ReactNode
}

interface State {
hasError: boolean
deviceInfo: string
}

class ErrorBoundary extends React.Component<Props, State> {
constructor(props: Props) {
super(props)
this.state = { hasError: false, deviceInfo: '' }
}

static getDerivedStateFromError(error: Error): State {
return { hasError: true, deviceInfo: '' }
}

componentDidCatch(error: Error, errorInfo: ErrorInfo) {
console.error('Error caught by ErrorBoundary:', error, errorInfo)
}

componentDidMount() {
const deviceInfo = `${mobileVendor} ${mobileModel}`
this.setState({ deviceInfo })
}

render() {
if (this.state.hasError) {
return (
<>
<h3>All apologies, the app is not yet available on this type of device.</h3>
<br />
<p>{this.state.deviceInfo}</p>
<br />
<p>Thank you for using the app from another device.</p>
<br />
<p>
Feel free to report this to Julien via <a href="https://matrix.to/#/@julienbrg:matrix.org">Element</a>,{' '}
<a href="https://warpcast.com/julien-">Farcaster</a>, <a href="https://t.me/julienbrg">Telegram</a>,{' '}
<a href="https://twitter.com/julienbrg">Twitter</a>, <a href="https://discordapp.com/users/julienbrg">Discord</a> or{' '}
<a href="https://www.linkedin.com/in/julienberanger/">LinkedIn</a>.
</p>
</>
)
}

return this.props.children
}
}

export default ErrorBoundary
19 changes: 11 additions & 8 deletions src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ChakraProvider } from '@chakra-ui/react'
import { Seo } from '../components/layout/Seo'
import { ERC20_CONTRACT_ADDRESS } from '../utils/erc20'
import { useIsMounted } from '../hooks/useIsMounted'
import ErrorBoundary from '../components/layout/ErrorBoundary'

export default function App({ Component, pageProps }: AppProps) {
useEffect(() => {
Expand All @@ -14,14 +15,16 @@ export default function App({ Component, pageProps }: AppProps) {

return (
<>
<ChakraProvider>
<Seo />
{isMounted && (
<Layout>
<Component {...pageProps} />
</Layout>
)}
</ChakraProvider>
<ErrorBoundary>
<ChakraProvider>
<Seo />
{isMounted && (
<Layout>
<Component {...pageProps} />
</Layout>
)}
</ChakraProvider>
</ErrorBoundary>
</>
)
}

0 comments on commit 263d594

Please sign in to comment.