-
-
Notifications
You must be signed in to change notification settings - Fork 419
feat(web): add reusable ErrorBoundary and wrap app routes #496
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
BrajamohanDas-afk
wants to merge
3
commits into
AOSSIE-Org:main
Choose a base branch
from
BrajamohanDas-afk:feat/error-boundary-eduaid-web
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,75 @@ | ||
| .error-boundary-container { | ||
| min-height: 220px; | ||
| width: 100%; | ||
| display: flex; | ||
| align-items: center; | ||
| justify-content: center; | ||
| padding: 24px; | ||
| box-sizing: border-box; | ||
| } | ||
|
|
||
| .error-boundary-card { | ||
| width: 100%; | ||
| max-width: 720px; | ||
| background: #141828; | ||
| border: 1px solid #2b3350; | ||
| border-radius: 12px; | ||
| padding: 20px; | ||
| color: #f3f6ff; | ||
| box-shadow: 0 6px 20px rgba(0, 0, 0, 0.25); | ||
| } | ||
|
|
||
| .error-boundary-title { | ||
| margin: 0 0 8px; | ||
| font-size: 1.25rem; | ||
| line-height: 1.3; | ||
| color: #ffb4b4; | ||
| } | ||
|
|
||
| .error-boundary-message { | ||
| margin: 0 0 14px; | ||
| font-size: 0.98rem; | ||
| color: #d7ddf5; | ||
| } | ||
|
|
||
| .error-boundary-details { | ||
| margin: 0 0 14px; | ||
| padding: 10px; | ||
| background: #0d1120; | ||
| border: 1px solid #2f395c; | ||
| border-radius: 8px; | ||
| color: #c6d0f8; | ||
| font-size: 0.78rem; | ||
| line-height: 1.4; | ||
| overflow: auto; | ||
| max-height: 220px; | ||
| white-space: pre-wrap; | ||
| overflow-wrap: anywhere; | ||
| } | ||
|
|
||
| .error-boundary-button { | ||
| border: none; | ||
| border-radius: 8px; | ||
| background: #4f7cff; | ||
| color: #ffffff; | ||
| padding: 10px 14px; | ||
| font-size: 0.9rem; | ||
| font-weight: 600; | ||
| cursor: pointer; | ||
| transition: background 0.2s ease; | ||
| } | ||
|
|
||
| .error-boundary-button:hover { | ||
| background: #3f68e0; | ||
| } | ||
|
|
||
| .error-boundary-button:focus { | ||
| outline: 2px solid #9ab4ff; | ||
| outline-offset: 2px; | ||
| } | ||
|
|
||
| .error-boundary-retry-limit { | ||
| margin: 8px 0 0; | ||
| color: #ffd3d3; | ||
| font-size: 0.9rem; | ||
| } |
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,97 @@ | ||
| import React, { Component } from "react"; | ||
| import "./ErrorBoundary.css"; | ||
|
|
||
| class ErrorBoundary extends Component { | ||
| static MAX_RETRIES = 3; | ||
|
|
||
| constructor(props) { | ||
| super(props); | ||
| this.state = { | ||
| hasError: false, | ||
| error: null, | ||
| errorInfo: null, | ||
| retryCount: 0, | ||
| }; | ||
| } | ||
|
|
||
| static getDerivedStateFromError(error) { | ||
| return { hasError: true, error }; | ||
| } | ||
|
|
||
| componentDidCatch(error, errorInfo) { | ||
| // Optional callback for external monitoring | ||
| if(typeof this.props.onError === "function") { | ||
| try{ | ||
| this.props.onError(error,errorInfo); | ||
| } catch(callbackError){ | ||
| console.error("ErrorBoundary onError callback failed:", callbackError); | ||
| } | ||
| } | ||
|
|
||
| // This is for debugging and future monitoring integration. | ||
| console.error("ErrorBoundary caught an error:", error); | ||
| console.error("Component stack:", errorInfo?.componentStack); | ||
|
|
||
| this.setState({ | ||
| errorInfo, | ||
| }); | ||
| } | ||
|
|
||
| handleRetry = () => { | ||
| if (this.state.retryCount >= ErrorBoundary.MAX_RETRIES) return; | ||
|
|
||
| this.setState((prevState) => ({ | ||
| hasError: false, | ||
| error: null, | ||
| errorInfo: null, | ||
| retryCount: prevState.retryCount + 1, | ||
| })); | ||
| }; | ||
|
|
||
| render() { | ||
| if (this.state.hasError) { | ||
| const canRetry = this.state.retryCount < ErrorBoundary.MAX_RETRIES; | ||
| const { | ||
| title = "Something went wrong in this section.", | ||
| message = "An unexpected error occurred. You can try again.", | ||
| showDetails = process.env.NODE_ENV !== "production", | ||
| } = this.props; | ||
|
|
||
| return ( | ||
| <div className="error-boundary-container" role="alert" aria-live="assertive"> | ||
| <div className="error-boundary-card"> | ||
| <h2 className="error-boundary-title">{title}</h2> | ||
| <p className="error-boundary-message">{message}</p> | ||
|
|
||
| {showDetails && this.state.error && ( | ||
| <pre className="error-boundary-details"> | ||
| {this.state.error.toString()} | ||
| {this.state.errorInfo?.componentStack | ||
| ? `\n\n${this.state.errorInfo.componentStack}` | ||
| : ""} | ||
| </pre> | ||
| )} | ||
|
|
||
| {canRetry ? ( | ||
| <button | ||
| type="button" | ||
| className="error-boundary-button" | ||
| onClick={this.handleRetry} | ||
| > | ||
| Try Again ({ErrorBoundary.MAX_RETRIES - this.state.retryCount} left) | ||
| </button> | ||
| ) : ( | ||
| <p className="error-boundary-retry-limit"> | ||
| Maximum retry attempts reached. Please refresh the page. | ||
| </p> | ||
| )} | ||
| </div> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| return this.props.children; | ||
| } | ||
| } | ||
|
|
||
| export default 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.