-
Notifications
You must be signed in to change notification settings - Fork 538
fix: Add react ErrorBoundary Component to prevent app crashes from unhandled errors #958
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
monu808
wants to merge
3
commits into
AOSSIE-Org:main
Choose a base branch
from
monu808:fix/error-app-crash
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import { Component, ErrorInfo, ReactNode } from 'react'; | ||
| import { AlertTriangle, RotateCw } from 'lucide-react'; | ||
| import { Button } from '@/components/ui/button'; | ||
|
|
||
| interface ErrorBoundaryProps { | ||
| children: ReactNode; | ||
| } | ||
|
|
||
| interface ErrorBoundaryState { | ||
| hasError: boolean; | ||
| error: Error | null; | ||
| isRetrying: boolean; | ||
| } | ||
|
|
||
| /** | ||
| * ErrorBoundary catches JavaScript errors anywhere in the child component tree, | ||
| * logs those errors, and displays a fallback UI instead of crashing the app. | ||
| */ | ||
| class ErrorBoundary extends Component<ErrorBoundaryProps, ErrorBoundaryState> { | ||
| constructor(props: ErrorBoundaryProps) { | ||
| super(props); | ||
| this.state = { hasError: false, error: null, isRetrying: false }; | ||
| } | ||
|
|
||
| static getDerivedStateFromError(error: Error): Partial<ErrorBoundaryState> { | ||
| return { hasError: true, error, isRetrying: false }; | ||
| } | ||
|
|
||
| componentDidCatch(error: Error, errorInfo: ErrorInfo): void { | ||
| console.error('ErrorBoundary caught an error:', error); | ||
| console.error('Component stack:', errorInfo.componentStack); | ||
| } | ||
|
|
||
| handleRetry = (): void => { | ||
| this.setState({ isRetrying: true }, () => { | ||
| // Small delay to show the spinner, then attempt recovery | ||
| setTimeout(() => { | ||
| this.setState({ hasError: false, error: null, isRetrying: false }); | ||
| }, 500); | ||
| }); | ||
| }; | ||
|
|
||
| render(): ReactNode { | ||
| if (this.state.hasError) { | ||
| return ( | ||
| <div className="bg-background flex min-h-screen flex-col items-center justify-center p-6"> | ||
| <div className="bg-card w-full max-w-md rounded-lg border p-8 text-center shadow-lg"> | ||
| <div className="bg-destructive/10 mx-auto mb-6 flex h-16 w-16 items-center justify-center rounded-full"> | ||
| <AlertTriangle className="text-destructive h-8 w-8" /> | ||
| </div> | ||
|
|
||
| <h1 className="text-foreground mb-2 text-2xl font-bold"> | ||
| Something went wrong | ||
| </h1> | ||
|
|
||
| <p className="text-muted-foreground mb-6"> | ||
| An unexpected error occurred. Please try reloading the | ||
| application. | ||
| </p> | ||
|
|
||
| <Button onClick={this.handleRetry} disabled={this.state.isRetrying}> | ||
| <RotateCw | ||
| className={`mr-2 h-4 w-4 ${this.state.isRetrying ? 'animate-spin' : ''}`} | ||
| /> | ||
| {this.state.isRetrying ? 'Reloading...' : 'Reload App'} | ||
| </Button> | ||
| </div> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| return this.props.children; | ||
| } | ||
| } | ||
|
|
||
| export default ErrorBoundary; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { default as ErrorBoundary } from './ErrorBoundary'; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.