-
Notifications
You must be signed in to change notification settings - Fork 2.8k
debug: Add ErrorBoundary component for better error handling #5085
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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,88 @@ | ||
| import React from "react" | ||
| import { render, screen } from "@testing-library/react" | ||
| import ErrorBoundary from "../components/ErrorBoundary" | ||
|
|
||
| // Mock telemetry client | ||
| vi.mock("@src/utils/TelemetryClient", () => ({ | ||
| telemetryClient: { | ||
| capture: vi.fn(), | ||
| }, | ||
| })) | ||
|
|
||
| // Mock translation function | ||
| vi.mock("react-i18next", () => { | ||
| const tFunction = (key: string) => key | ||
| return { | ||
| withTranslation: () => (Component: any) => { | ||
| const MockedComponent = (props: any) => { | ||
| return <Component t={tFunction} i18n={{ t: tFunction }} tReady {...props} /> | ||
| } | ||
| MockedComponent.displayName = `withTranslation(${Component.displayName || Component.name || "Component"})` | ||
| return MockedComponent | ||
| }, | ||
| } | ||
| }) | ||
|
|
||
| // Test component that can throw errors on demand | ||
| const ErrorThrower = ({ shouldThrow = false, message = "Test error" }: { shouldThrow?: boolean; message?: string }) => { | ||
| if (shouldThrow) { | ||
| throw new Error(message) | ||
| } | ||
| return <div>No error</div> | ||
| } | ||
|
|
||
| describe("ErrorBoundary", () => { | ||
| // Suppress console errors during tests | ||
| beforeEach(() => { | ||
| vi.spyOn(console, "error").mockImplementation(() => {}) | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| vi.restoreAllMocks() | ||
| }) | ||
|
|
||
| it("renders children when there is no error", () => { | ||
| render( | ||
| <ErrorBoundary> | ||
| <div data-testid="test-child">Test Content</div> | ||
| </ErrorBoundary>, | ||
| ) | ||
|
|
||
| expect(screen.getByTestId("test-child")).toBeInTheDocument() | ||
| expect(screen.getByText("Test Content")).toBeInTheDocument() | ||
| }) | ||
|
|
||
| it("renders error UI when a child component throws", () => { | ||
| vi.stubEnv("PKG_VERSION", "1.2.3") | ||
|
|
||
| // Using the React testing library's render method with an error boundary is tricky | ||
| // We need to catch and ignore the error during the test | ||
| const spy = vi.spyOn(console, "error").mockImplementation(() => {}) | ||
|
|
||
| render( | ||
| <ErrorBoundary> | ||
| <ErrorThrower shouldThrow={true} message="Test component error" /> | ||
| </ErrorBoundary>, | ||
| ) | ||
|
|
||
| // Verify error boundary elements are displayed - using partial matchers to account for version info | ||
| expect(screen.getByText(/errorBoundary.title/)).toBeInTheDocument() | ||
|
|
||
| // Check for the GitHub link | ||
| const githubLink = screen.getByRole("link", { name: /errorBoundary.githubText/ }) | ||
| expect(githubLink).toBeInTheDocument() | ||
| expect(githubLink).toHaveAttribute("href", "https://github.com/RooCodeInc/Roo-Code/issues") | ||
|
|
||
| // Check for other error boundary elements | ||
| expect(screen.getByText(/errorBoundary.copyInstructions/)).toBeInTheDocument() | ||
| expect(screen.getByText(/errorBoundary.errorStack/)).toBeInTheDocument() | ||
|
|
||
| // In test environments, the componentStack might not always be available | ||
| // so we don't check for it to make the test more reliable | ||
|
|
||
| // The test error message should be included in the error display | ||
| expect(screen.getByText(/Test component error/)).toBeInTheDocument() | ||
|
|
||
| spy.mockRestore() | ||
| }) | ||
| }) |
Oops, something went wrong.
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.