-
Notifications
You must be signed in to change notification settings - Fork 18
fix: handle NotAuthenticatedError in error boundary gracefully #2086
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
jonathanlab
wants to merge
1
commit into
main
Choose a base branch
from
posthog-code/fix-incorrect-error-boundary-for-unauthenticated-users
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.
+242
−39
Open
Changes from all commits
Commits
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
162 changes: 162 additions & 0 deletions
162
apps/code/src/renderer/components/ErrorBoundary.test.tsx
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,162 @@ | ||
| import { Theme } from "@radix-ui/themes"; | ||
| import { isNotAuthenticatedError, NotAuthenticatedError } from "@shared/errors"; | ||
| import { render, screen } from "@testing-library/react"; | ||
| import userEvent from "@testing-library/user-event"; | ||
| import type { ReactNode } from "react"; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
| import { ErrorBoundary } from "./ErrorBoundary"; | ||
|
|
||
| vi.mock("@utils/analytics", () => ({ | ||
| captureException: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("@utils/logger", () => ({ | ||
| logger: { | ||
| scope: () => ({ | ||
| error: vi.fn(), | ||
| warn: vi.fn(), | ||
| info: vi.fn(), | ||
| debug: vi.fn(), | ||
| }), | ||
| }, | ||
| })); | ||
|
|
||
| import { captureException } from "@utils/analytics"; | ||
|
|
||
| function Thrower({ error }: { error: Error | null }) { | ||
| if (error) throw error; | ||
| return <div>ok</div>; | ||
| } | ||
|
|
||
| function Boundary(props: { | ||
| children: ReactNode; | ||
| resetKey?: unknown; | ||
| shouldSuppress?: (e: Error) => boolean; | ||
| fallback?: ReactNode; | ||
| }) { | ||
| return ( | ||
| <Theme> | ||
| <ErrorBoundary | ||
| resetKey={props.resetKey} | ||
| shouldSuppress={props.shouldSuppress} | ||
| fallback={props.fallback} | ||
| > | ||
| {props.children} | ||
| </ErrorBoundary> | ||
| </Theme> | ||
| ); | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| vi.spyOn(console, "error").mockImplementation(() => {}); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| vi.mocked(captureException).mockClear(); | ||
| }); | ||
|
|
||
| describe("ErrorBoundary", () => { | ||
| it("renders children when no error is thrown", () => { | ||
| render( | ||
| <Boundary> | ||
| <Thrower error={null} /> | ||
| </Boundary>, | ||
| ); | ||
| expect(screen.getByText("ok")).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("renders the default fallback UI on error and reports telemetry", () => { | ||
| render( | ||
| <Boundary> | ||
| <Thrower error={new Error("boom")} /> | ||
| </Boundary>, | ||
| ); | ||
| expect(screen.getByText("Something went wrong")).toBeInTheDocument(); | ||
| expect(screen.getByText("boom")).toBeInTheDocument(); | ||
| expect(captureException).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it("renders custom fallback when provided", () => { | ||
| render( | ||
| <Boundary fallback={<div>custom fallback</div>}> | ||
| <Thrower error={new Error("boom")} /> | ||
| </Boundary>, | ||
| ); | ||
| expect(screen.getByText("custom fallback")).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("suppresses errors that match shouldSuppress (renders null, no telemetry)", () => { | ||
| render( | ||
| <Boundary shouldSuppress={isNotAuthenticatedError}> | ||
| <Thrower error={new NotAuthenticatedError()} /> | ||
| </Boundary>, | ||
| ); | ||
| expect(screen.queryByText("Something went wrong")).not.toBeInTheDocument(); | ||
| expect(screen.queryByText("ok")).not.toBeInTheDocument(); | ||
| expect(captureException).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("does not suppress non-matching errors", () => { | ||
| render( | ||
| <Boundary shouldSuppress={isNotAuthenticatedError}> | ||
| <Thrower error={new Error("other failure")} /> | ||
| </Boundary>, | ||
| ); | ||
| expect(screen.getByText("Something went wrong")).toBeInTheDocument(); | ||
| expect(captureException).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it("clears error state when resetKey changes", () => { | ||
| const { rerender } = render( | ||
| <Boundary resetKey="a"> | ||
| <Thrower error={new Error("boom")} /> | ||
| </Boundary>, | ||
| ); | ||
| expect(screen.getByText("Something went wrong")).toBeInTheDocument(); | ||
|
|
||
| rerender( | ||
| <Boundary resetKey="b"> | ||
| <Thrower error={null} /> | ||
| </Boundary>, | ||
| ); | ||
| expect(screen.queryByText("Something went wrong")).not.toBeInTheDocument(); | ||
| expect(screen.getByText("ok")).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("recovers via retry button", async () => { | ||
| const user = userEvent.setup(); | ||
| const { rerender } = render( | ||
| <Boundary> | ||
| <Thrower error={new Error("boom")} /> | ||
| </Boundary>, | ||
| ); | ||
| expect(screen.getByText("Something went wrong")).toBeInTheDocument(); | ||
|
|
||
| rerender( | ||
| <Boundary> | ||
| <Thrower error={null} /> | ||
| </Boundary>, | ||
| ); | ||
| await user.click(screen.getByRole("button", { name: /try again/i })); | ||
| expect(screen.getByText("ok")).toBeInTheDocument(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("isNotAuthenticatedError", () => { | ||
| it("matches NotAuthenticatedError instances", () => { | ||
| expect(isNotAuthenticatedError(new NotAuthenticatedError())).toBe(true); | ||
| }); | ||
|
|
||
| it("matches plain objects with the same name (e.g. tRPC-serialized errors)", () => { | ||
| expect(isNotAuthenticatedError({ name: "NotAuthenticatedError" })).toBe( | ||
| true, | ||
| ); | ||
| }); | ||
|
|
||
| it("does not match unrelated errors", () => { | ||
| expect(isNotAuthenticatedError(new Error("Not authenticated"))).toBe(false); | ||
| expect(isNotAuthenticatedError(null)).toBe(false); | ||
| expect(isNotAuthenticatedError("Not authenticated")).toBe(false); | ||
| }); | ||
| }); | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isNotAuthenticatedErrorThe three
itblocks below cover five distinct input/expectation pairs and would be cleaner as a singleit.eachtable. The current structure also buries the threefalseassertions inside one test body, making it easy to miss a case. Usingit.eachmaps each input to its expected result in one place and is the team's stated preference for tests like these.Prompt To Fix With AI
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!