-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Site Editor: Add error boundary (#33921)
- Loading branch information
Showing
3 changed files
with
169 additions
and
82 deletions.
There are no files selected for viewing
This file contains 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 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, | ||
}; | ||
} | ||
|
||
static getDerivedStateFromError( error ) { | ||
return { error }; | ||
} | ||
|
||
reboot() { | ||
this.props.onError(); | ||
} | ||
|
||
render() { | ||
const { error } = this.state; | ||
if ( ! error ) { | ||
return this.props.children; | ||
} | ||
|
||
return ( | ||
<Warning | ||
className="editor-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> | ||
); | ||
} | ||
} |
This file contains 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