-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Add error boundaries to widget screens #33771
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
6 commits
Select commit
Hold shift + click to select a range
692fd2f
Add error boundary to edit widgets screen
talldan 5b7a7dc
Add error boundary to customize widgets
talldan 7d607ff
Refactor sidebar controls provider to application level so that its s…
talldan 78e821f
Revert "Refactor sidebar controls provider to application level so th…
talldan b1add9a
Remove rebootability from customize widgets
talldan e652b26
Remove debug code
talldan 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
50 changes: 50 additions & 0 deletions
50
packages/customize-widgets/src/components/error-boundary/index.js
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,50 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { Component } from '@wordpress/element'; | ||
| import { __ } from '@wordpress/i18n'; | ||
| import { Button } from '@wordpress/components'; | ||
| import { Warning } from '@wordpress/block-editor'; | ||
| import { useCopyToClipboard } from '@wordpress/compose'; | ||
|
|
||
| function CopyButton( { text, children } ) { | ||
| const ref = useCopyToClipboard( text ); | ||
| return ( | ||
| <Button variant="secondary" ref={ ref }> | ||
| { children } | ||
| </Button> | ||
| ); | ||
| } | ||
|
|
||
| export default class ErrorBoundary extends Component { | ||
| constructor() { | ||
| super( ...arguments ); | ||
| this.state = { | ||
| error: null, | ||
| }; | ||
| } | ||
|
|
||
| componentDidCatch( error ) { | ||
| this.setState( { error } ); | ||
| } | ||
|
|
||
| render() { | ||
| const { error } = this.state; | ||
| if ( ! error ) { | ||
| return this.props.children; | ||
| } | ||
|
|
||
| return ( | ||
| <Warning | ||
| className="customize-widgets-error-boundary" | ||
| actions={ [ | ||
| <CopyButton key="copy-error" text={ error.stack }> | ||
| { __( 'Copy Error' ) } | ||
| </CopyButton>, | ||
| ] } | ||
| > | ||
| { __( 'The editor has encountered an unexpected error.' ) } | ||
| </Warning> | ||
| ); | ||
| } | ||
| } |
64 changes: 64 additions & 0 deletions
64
packages/edit-widgets/src/components/error-boundary/index.js
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 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { Component } from '@wordpress/element'; | ||
| import { __ } from '@wordpress/i18n'; | ||
| import { Button } from '@wordpress/components'; | ||
| import { Warning } from '@wordpress/block-editor'; | ||
| import { useCopyToClipboard } from '@wordpress/compose'; | ||
|
|
||
| function CopyButton( { text, children } ) { | ||
| const ref = useCopyToClipboard( text ); | ||
| return ( | ||
| <Button variant="secondary" ref={ ref }> | ||
| { children } | ||
| </Button> | ||
| ); | ||
| } | ||
|
|
||
| export default class ErrorBoundary extends Component { | ||
| constructor() { | ||
| super( ...arguments ); | ||
|
|
||
| this.reboot = this.reboot.bind( this ); | ||
|
|
||
| this.state = { | ||
| error: null, | ||
| }; | ||
| } | ||
|
|
||
| componentDidCatch( error ) { | ||
| this.setState( { error } ); | ||
| } | ||
|
|
||
| reboot() { | ||
| this.props.onError(); | ||
| } | ||
|
|
||
| render() { | ||
| const { error } = this.state; | ||
| if ( ! error ) { | ||
| return this.props.children; | ||
| } | ||
|
|
||
| return ( | ||
| <Warning | ||
| className="edit-widgets-error-boundary" | ||
| actions={ [ | ||
| <Button | ||
| key="recovery" | ||
| onClick={ this.reboot } | ||
| variant="secondary" | ||
| > | ||
| { __( 'Attempt Recovery' ) } | ||
| </Button>, | ||
| <CopyButton key="copy-error" text={ error.stack }> | ||
| { __( 'Copy Error' ) } | ||
| </CopyButton>, | ||
| ] } | ||
| > | ||
| { __( 'The editor has encountered an unexpected error.' ) } | ||
| </Warning> | ||
| ); | ||
| } | ||
| } |
7 changes: 7 additions & 0 deletions
7
packages/edit-widgets/src/components/error-boundary/style.scss
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,7 @@ | ||
| .edit-widgets-error-boundary { | ||
| margin: auto; | ||
| max-width: 780px; | ||
| padding: 20px; | ||
| margin-top: 60px; | ||
| box-shadow: $shadow-modal; | ||
| } |
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
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.
I wonder if we should add this to the root of the editor, in hope of being able to correctly reset all the states. Not sure if it would work though.
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.
I gave it a try during development, but yeah, doesn't quite work. I think we'll need something a little different to handle the portals properly in the customizer editor.
Uh oh!
There was an error while loading. Please reload this page.
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.
I don't think it's a problem with the portals, but rather that we(I) don't properly clear/reset the states of controls after unmounted 😅. It didn't occur to me that we would ever need to unmount the component. Probably just have to add some
useEffectin the components.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.
The error boundary UI also needs to render in the right place (which is probably in the portal).
7d607ff has my first attempt to solve the
activeSidebarControlstate problems, but it was a bit naive because even the context is lost when unmounting/remounting the app.Moving the active sidebar control to store state so that it persists across umounting/remounting the whole application might be the easiest option.