-
Notifications
You must be signed in to change notification settings - Fork 8
feat: Add error boundary for diagram rendering #124
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
kumaradityaraj
wants to merge
10
commits into
serverlessworkflow:main
Choose a base branch
from
kumaradityaraj:errorBoundary
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
10 commits
Select commit
Hold shift + click to select a range
1db5249
Error boundary
kumaradityaraj 4b0948d
add liscense
kumaradityaraj 1940fb6
copilot suggestion
kumaradityaraj dcdf352
Resolving merge conflict
kumaradityaraj 1914a94
Removing comment
kumaradityaraj 5336f9e
Add DiagramEditorErrorBoundary.stories.tsx
fantonangeli 868c3a9
Merge pull request #4 from fantonangeli/kumaradityaraj-errorBoundary-…
kumaradityaraj 3a2206c
removing redundant test case
kumaradityaraj 19d2eb7
dummy push
kumaradityaraj 50d4b0e
resolving format bugs
kumaradityaraj 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
Some comments aren't visible on the classic Files Changed page.
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
64 changes: 64 additions & 0 deletions
64
...ess-workflow-diagram-editor/src/diagram-editor/error-pages/DiagramEditorErrorBoundary.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,64 @@ | ||
| /* | ||
| * Copyright 2021-Present The Serverless Workflow Specification Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import * as React from "react"; | ||
| import { ErrorPage } from "./ErrorPage"; | ||
|
|
||
| type State = { | ||
| hasError: boolean; | ||
| error?: unknown; | ||
| }; | ||
|
|
||
| type DiagramEditorErrorBoundaryProps = { | ||
| children: React.ReactNode; | ||
| title?: string; | ||
| message?: string; | ||
| resetKey?: string; | ||
| }; | ||
|
|
||
| export class DiagramEditorErrorBoundary extends React.Component< | ||
| DiagramEditorErrorBoundaryProps, | ||
| State | ||
| > { | ||
| constructor(props: DiagramEditorErrorBoundaryProps) { | ||
| super(props); | ||
| this.state = { hasError: false }; | ||
| } | ||
|
|
||
| static getDerivedStateFromError(error: unknown): State { | ||
| return { hasError: true, error }; | ||
| } | ||
|
|
||
| componentDidUpdate(prevProps: DiagramEditorErrorBoundaryProps) { | ||
| if (this.state.hasError && prevProps.resetKey !== this.props.resetKey) { | ||
| this.setState({ hasError: false, error: undefined }); | ||
| } | ||
|
kumaradityaraj marked this conversation as resolved.
|
||
| } | ||
|
|
||
| render() { | ||
| if (this.state.hasError) { | ||
| return ( | ||
| <ErrorPage | ||
| title={this.props.title ?? "Something went wrong"} | ||
|
kumaradityaraj marked this conversation as resolved.
|
||
| message={this.props.message ?? "An unexpected error occurred"} | ||
| snippet={this.state.error instanceof Error ? this.state.error.message : undefined} | ||
|
kumaradityaraj marked this conversation as resolved.
|
||
| /> | ||
| ); | ||
| } | ||
|
|
||
| return this.props.children; | ||
| } | ||
| } | ||
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
60 changes: 60 additions & 0 deletions
60
packages/serverless-workflow-diagram-editor/stories/DiagramEditorErrorBoundary.stories.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,60 @@ | ||
| /* | ||
| * Copyright 2021-Present The Serverless Workflow Specification Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import type { Meta, StoryObj } from "@storybook/react-vite"; | ||
| import { DiagramEditorErrorBoundary } from "../src/diagram-editor/error-pages/DiagramEditorErrorBoundary"; | ||
| import { ColorMode } from "../src/types/colorMode"; | ||
|
|
||
| type DiagramEditorErrorBoundaryProps = { | ||
| title?: string; | ||
| message?: string; | ||
| resetKey?: string; | ||
| }; | ||
|
|
||
| type DiagramEditorErrorBoundaryStoryProps = DiagramEditorErrorBoundaryProps & { | ||
| colorMode?: ColorMode; | ||
| }; | ||
|
|
||
| const ThrowError = ({ message = "Test error message" }: { message?: string }) => { | ||
| throw new Error(message); | ||
| }; | ||
|
|
||
| const meta = { | ||
| title: "Example/DiagramEditorErrorBoundary", | ||
| component: DiagramEditorErrorBoundary, | ||
| tags: ["autodocs"], | ||
| parameters: { | ||
| layout: "fullscreen", | ||
| }, | ||
| args: {}, | ||
| } satisfies Meta<DiagramEditorErrorBoundaryStoryProps>; | ||
|
|
||
| export default meta; | ||
| type Story = StoryObj<typeof meta>; | ||
|
|
||
| export const WithDefaults: Story = { | ||
| args: { | ||
| children: <ThrowError />, | ||
| }, | ||
| }; | ||
|
kumaradityaraj marked this conversation as resolved.
|
||
|
|
||
| export const WithErrorCustomMessage: Story = { | ||
| args: { | ||
| title: "Custom Error Title", | ||
| message: "This is a custom error message", | ||
| children: <ThrowError message="Custom error details in snippet" />, | ||
| }, | ||
| }; | ||
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
95 changes: 95 additions & 0 deletions
95
...kflow-diagram-editor/tests/diagram-editor/error-pages/DiagramEditorErrorBoundary.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,95 @@ | ||
| /* | ||
| * Copyright 2021-Present The Serverless Workflow Specification Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import { render, screen } from "@testing-library/react"; | ||
| import { DiagramEditorErrorBoundary } from "../../../src/diagram-editor/error-pages/DiagramEditorErrorBoundary"; | ||
| import { describe, expect, it } from "vitest"; | ||
|
|
||
| const ThrowError = ({ message = "Test error" }: { message?: string }) => { | ||
| throw new Error(message); | ||
| }; | ||
|
|
||
| const SafeComponent = () => <div>Safe Content</div>; | ||
|
|
||
| describe("DiagramEditorErrorBoundary", () => { | ||
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
kumaradityaraj marked this conversation as resolved.
|
||
|
|
||
| it("renders children when no error occurs", () => { | ||
| render( | ||
| <DiagramEditorErrorBoundary> | ||
| <SafeComponent /> | ||
| </DiagramEditorErrorBoundary>, | ||
| ); | ||
|
|
||
| expect(screen.getByText("Safe Content")).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("renders fallback UI when child throws", () => { | ||
| const spy = vi.spyOn(console, "error").mockImplementation(() => {}); | ||
|
|
||
| render( | ||
| <DiagramEditorErrorBoundary title="Error Title" message="Error Message"> | ||
| <ThrowError /> | ||
| </DiagramEditorErrorBoundary>, | ||
| ); | ||
|
|
||
| expect(screen.getByText("Error Title")).toBeInTheDocument(); | ||
| expect(screen.getByText("Error Message")).toBeInTheDocument(); | ||
| expect(screen.getByText("Test error")).toBeInTheDocument(); | ||
|
|
||
| spy.mockRestore(); | ||
| }); | ||
|
|
||
| it("uses default fallback values when props not provided", () => { | ||
| const spy = vi.spyOn(console, "error").mockImplementation(() => {}); | ||
|
|
||
| render( | ||
| <DiagramEditorErrorBoundary> | ||
| <ThrowError /> | ||
| </DiagramEditorErrorBoundary>, | ||
| ); | ||
|
|
||
| expect(screen.getByText("Something went wrong")).toBeInTheDocument(); | ||
|
|
||
| expect(screen.getByText("An unexpected error occurred")).toBeInTheDocument(); | ||
|
|
||
| spy.mockRestore(); | ||
| }); | ||
|
|
||
| it("resets error boundary when resetKey changes", () => { | ||
| const spy = vi.spyOn(console, "error").mockImplementation(() => {}); | ||
|
|
||
| const { rerender } = render( | ||
| <DiagramEditorErrorBoundary resetKey="key-1"> | ||
| <ThrowError /> | ||
| </DiagramEditorErrorBoundary>, | ||
| ); | ||
|
|
||
| expect(screen.getByText(/Something went wrong/i)).toBeInTheDocument(); | ||
|
|
||
| rerender( | ||
| <DiagramEditorErrorBoundary resetKey="key-2"> | ||
| <SafeComponent /> | ||
| </DiagramEditorErrorBoundary>, | ||
| ); | ||
|
|
||
| expect(screen.getByText("Safe Content")).toBeInTheDocument(); | ||
|
|
||
| spy.mockRestore(); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
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.