-
Notifications
You must be signed in to change notification settings - Fork 0
Add error boundary with diagnostics #58
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
moshloop
wants to merge
2
commits into
main
Choose a base branch
from
feat/error-boundary-diagnostics-dkht3fixyv34
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
2 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,111 @@ | ||
| import type { Meta, StoryObj } from "@storybook/react-vite"; | ||
| import { expect, userEvent, within } from "storybook/test"; | ||
| import { ErrorWrapper } from "./ErrorWrapper"; | ||
|
|
||
| const STORY_ERROR = new Error("The dashboard request failed with HTTP 502", { | ||
| cause: "upstream service unavailable", | ||
| }); | ||
| STORY_ERROR.stack = [ | ||
| "Error: The dashboard request failed with HTTP 502", | ||
| " at loadDashboard (src/pages/Dashboard.tsx:84:15)", | ||
| " at Dashboard (src/pages/Dashboard.tsx:27:3)", | ||
| ].join("\n"); | ||
|
|
||
| function BrokenDashboard(): never { | ||
| throw STORY_ERROR; | ||
| } | ||
|
|
||
| function mockClipboard(writeText: (text: string) => Promise<void>): () => void { | ||
| const descriptor = Object.getOwnPropertyDescriptor(navigator, "clipboard"); | ||
| Object.defineProperty(navigator, "clipboard", { | ||
| value: { writeText }, | ||
| configurable: true, | ||
| }); | ||
| return () => { | ||
| if (descriptor) { | ||
| Object.defineProperty(navigator, "clipboard", descriptor); | ||
| } else { | ||
| Reflect.deleteProperty(navigator, "clipboard"); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| const meta = { | ||
| title: "Components/ErrorWrapper", | ||
| component: ErrorWrapper, | ||
| tags: ["autodocs"], | ||
| parameters: { | ||
| layout: "fullscreen", | ||
| docs: { | ||
| description: { | ||
| component: | ||
| "Full-page React error boundary with normalized diagnostics and a support-ready copy action.", | ||
| }, | ||
| }, | ||
| }, | ||
| argTypes: { | ||
| children: { control: false }, | ||
| onError: { control: false }, | ||
| }, | ||
| } satisfies Meta<typeof ErrorWrapper>; | ||
|
|
||
| export default meta; | ||
| type Story = StoryObj<typeof meta>; | ||
|
|
||
| export const Default: Story = { | ||
| render: () => ( | ||
| <ErrorWrapper> | ||
| <BrokenDashboard /> | ||
| </ErrorWrapper> | ||
| ), | ||
| play: async ({ canvasElement }) => { | ||
| const canvas = within(canvasElement); | ||
| await expect(canvas.getByRole("alert")).toBeInTheDocument(); | ||
| await expect( | ||
| canvas.getByRole("heading", { name: "Something went wrong" }), | ||
| ).toBeInTheDocument(); | ||
| }, | ||
| }; | ||
|
|
||
| export const CopySuccess: Story = { | ||
| ...Default, | ||
| play: async ({ canvasElement }) => { | ||
| const canvas = within(canvasElement); | ||
| const restoreClipboard = mockClipboard(() => Promise.resolve()); | ||
| try { | ||
| await userEvent.click( | ||
| canvas.getByRole("button", { name: "Copy error details" }), | ||
| ); | ||
| await expect( | ||
| canvas.getByRole("button", { name: "Copied" }), | ||
| ).toBeInTheDocument(); | ||
| await expect( | ||
| canvas.getByText("Error details copied to clipboard."), | ||
| ).toBeInTheDocument(); | ||
| } finally { | ||
| restoreClipboard(); | ||
| } | ||
| }, | ||
| }; | ||
|
|
||
| export const ClipboardFailure: Story = { | ||
| ...Default, | ||
| play: async ({ canvasElement }) => { | ||
| const canvas = within(canvasElement); | ||
| const restoreClipboard = mockClipboard(() => | ||
| Promise.reject(new Error("Clipboard access denied")), | ||
| ); | ||
| try { | ||
| await userEvent.click( | ||
| canvas.getByRole("button", { name: "Copy error details" }), | ||
| ); | ||
| await expect( | ||
| canvas.getByText( | ||
| "Clipboard access failed. Expand the error details to copy individual values.", | ||
| ), | ||
| ).toBeInTheDocument(); | ||
| } finally { | ||
| restoreClipboard(); | ||
| } | ||
| }, | ||
| }; | ||
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,111 @@ | ||
| import { fireEvent, render, screen, waitFor } from "@testing-library/react"; | ||
| import { afterEach, describe, expect, it, vi } from "vitest"; | ||
| import { ErrorWrapper } from "./ErrorWrapper"; | ||
|
|
||
| function BrokenPage(): never { | ||
| throw new Error("Unable to load the account dashboard", { | ||
| cause: "upstream request returned HTTP 502", | ||
| }); | ||
| } | ||
|
|
||
| const originalClipboardDescriptor = Object.getOwnPropertyDescriptor( | ||
| navigator, | ||
| "clipboard", | ||
| ); | ||
| const originalPageUrl = window.location.href; | ||
|
|
||
| describe("ErrorWrapper", () => { | ||
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| if (originalClipboardDescriptor) { | ||
| Object.defineProperty( | ||
| navigator, | ||
| "clipboard", | ||
| originalClipboardDescriptor, | ||
| ); | ||
| } else { | ||
| Reflect.deleteProperty(navigator, "clipboard"); | ||
| } | ||
| window.history.replaceState({}, "", originalPageUrl); | ||
| }); | ||
|
|
||
| it("renders a full-page diagnostic fallback and copies a support-ready report", async () => { | ||
| vi.spyOn(console, "error").mockImplementation(() => {}); | ||
| window.history.replaceState( | ||
| {}, | ||
| "", | ||
| "/accounts?access_token=secret#authorization-code", | ||
| ); | ||
| const writeText = vi.fn().mockResolvedValue(undefined); | ||
| Object.defineProperty(navigator, "clipboard", { | ||
| value: { writeText }, | ||
| configurable: true, | ||
| }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| render( | ||
| <ErrorWrapper> | ||
| <BrokenPage /> | ||
| </ErrorWrapper>, | ||
| ); | ||
|
|
||
| const fallback = screen.getByRole("alert"); | ||
| expect(fallback).toHaveClass("min-h-dvh"); | ||
| expect( | ||
| screen.getByRole("heading", { name: "Something went wrong" }), | ||
| ).toBeInTheDocument(); | ||
| expect(fallback).toHaveTextContent("Unable to load the account dashboard"); | ||
|
|
||
| fireEvent.click(screen.getByRole("button", { name: "Copy error details" })); | ||
|
|
||
| await waitFor(() => expect(writeText).toHaveBeenCalledOnce()); | ||
| const report = writeText.mock.calls[0]?.[0]; | ||
| expect(report).toEqual( | ||
| expect.stringContaining("Error: Unable to load the account dashboard"), | ||
| ); | ||
| expect(report).toEqual( | ||
| expect.stringContaining("Cause: upstream request returned HTTP 502"), | ||
| ); | ||
| expect(report).toEqual( | ||
| expect.stringContaining(`Page: ${window.location.origin}/accounts`), | ||
| ); | ||
| expect(report).not.toEqual(expect.stringContaining("access_token")); | ||
| expect(report).not.toEqual(expect.stringContaining("authorization-code")); | ||
| expect(report).toEqual(expect.stringContaining("React component stack:")); | ||
| expect(screen.getByRole("button", { name: "Copied" })).toBeInTheDocument(); | ||
| expect(liveRegion(fallback)).toHaveTextContent( | ||
| "Error details copied to clipboard.", | ||
| ); | ||
| }); | ||
|
|
||
| it("announces a clipboard failure in the live region", async () => { | ||
| vi.spyOn(console, "error").mockImplementation(() => {}); | ||
| const writeText = vi.fn().mockRejectedValue(new Error("denied")); | ||
| Object.defineProperty(navigator, "clipboard", { | ||
| value: { writeText }, | ||
| configurable: true, | ||
| }); | ||
|
|
||
| render( | ||
| <ErrorWrapper> | ||
| <BrokenPage /> | ||
| </ErrorWrapper>, | ||
| ); | ||
|
|
||
| const fallback = screen.getByRole("alert"); | ||
| expect(liveRegion(fallback)).toHaveTextContent(""); | ||
|
|
||
| fireEvent.click(screen.getByRole("button", { name: "Copy error details" })); | ||
|
|
||
| await waitFor(() => | ||
| expect(liveRegion(fallback)).toHaveTextContent( | ||
| "Clipboard access failed. Expand the error details to copy individual values.", | ||
| ), | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| function liveRegion(fallback: HTMLElement): HTMLElement { | ||
| const region = fallback.querySelector<HTMLElement>('[aria-live="polite"]'); | ||
| if (!region) throw new Error("ErrorWrapper is missing its live region"); | ||
| return region; | ||
| } | ||
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.