Skip to content

Commit 9e241ac

Browse files
committed
Added error handler components.
1 parent 5403271 commit 9e241ac

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

src/components/ErrorBoundary.tsx

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'use client' // This must be a Client Component
2+
3+
import React, { Component, ErrorInfo, ReactNode } from 'react'
4+
import { reportError } from '@/lib/errorReportingService' // Adjust path if needed
5+
6+
interface Props {
7+
children: ReactNode
8+
}
9+
10+
interface State {
11+
hasError: boolean
12+
}
13+
14+
class ErrorBoundary extends Component<Props, State> {
15+
constructor(props: Props) {
16+
super(props)
17+
this.state = { hasError: false }
18+
}
19+
20+
static getDerivedStateFromError(_: Error): State {
21+
// Update state so the next render will show the fallback UI.
22+
return { hasError: true }
23+
}
24+
25+
componentDidCatch(error: Error, errorInfo: ErrorInfo) {
26+
// Log the error to your reporting service
27+
reportError(error, { errorInfo })
28+
}
29+
30+
render() {
31+
if (this.state.hasError) {
32+
// You can render any custom fallback UI
33+
return (
34+
<div style={{ padding: '2rem', textAlign: 'center' }}>
35+
<h2>Something went wrong.</h2>
36+
<p>
37+
An unexpected error has occurred. We have been notified and are working to fix the
38+
issue.
39+
</p>
40+
<button onClick={() => this.setState({ hasError: false })}>Try again</button>
41+
</div>
42+
)
43+
}
44+
45+
return this.props.children
46+
}
47+
}
48+
49+
export default ErrorBoundary

src/components/GlobalErrorHandler.tsx

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use client' // This component must be a Client Component
2+
3+
import { useEffect } from 'react'
4+
import { reportError } from '@/lib/errorReportingService' // Adjust path if needed
5+
6+
export default function GlobalErrorHandler() {
7+
useEffect(() => {
8+
// --- Handler for uncaught errors ---
9+
const handleError = (event: ErrorEvent) => {
10+
event.preventDefault()
11+
reportError(event.error, { isFatal: true })
12+
}
13+
14+
// --- Handler for unhandled promise rejections ---
15+
const handleRejection = (event: PromiseRejectionEvent) => {
16+
event.preventDefault()
17+
// A rejection reason can be anything, but we'll try to handle it as an Error
18+
const error =
19+
event.reason instanceof Error
20+
? event.reason
21+
: new Error(`Promise rejected with a non-error value: ${String(event.reason)}`)
22+
23+
reportError(error, { isFatal: true })
24+
}
25+
26+
window.addEventListener('error', handleError)
27+
window.addEventListener('unhandledrejection', handleRejection)
28+
29+
// --- Cleanup function ---
30+
// This will run when the component unmounts
31+
return () => {
32+
window.removeEventListener('error', handleError)
33+
window.removeEventListener('unhandledrejection', handleRejection)
34+
}
35+
}, []) // The empty dependency array ensures this runs only once
36+
37+
// This component renders nothing to the DOM
38+
return null
39+
}

0 commit comments

Comments
 (0)