Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add error boundary to edit widgets screen
  • Loading branch information
talldan committed Jul 29, 2021
commit 692fd2f67a2647fc1bdea50e13aa59fbe59525a3
64 changes: 64 additions & 0 deletions packages/edit-widgets/src/components/error-boundary/index.js
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>
);
}
}
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;
}
25 changes: 14 additions & 11 deletions packages/edit-widgets/src/components/layout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,27 @@ import { PluginArea } from '@wordpress/plugins';
/**
* Internal dependencies
*/
import ErrorBoundary from '../error-boundary';
import WidgetAreasBlockEditorProvider from '../widget-areas-block-editor-provider';
import Sidebar from '../sidebar';
import Interface from './interface';
import UnsavedChangesWarning from './unsaved-changes-warning';
import WelcomeGuide from '../welcome-guide';

function Layout( { blockEditorSettings } ) {
function Layout( { blockEditorSettings, onError } ) {
return (
<WidgetAreasBlockEditorProvider
blockEditorSettings={ blockEditorSettings }
>
<Interface blockEditorSettings={ blockEditorSettings } />
<Sidebar />
<Popover.Slot />
<PluginArea />
<UnsavedChangesWarning />
<WelcomeGuide />
</WidgetAreasBlockEditorProvider>
<ErrorBoundary onError={ onError }>
<WidgetAreasBlockEditorProvider
blockEditorSettings={ blockEditorSettings }
>
<Interface blockEditorSettings={ blockEditorSettings } />
<Sidebar />
<Popover.Slot />
<PluginArea />
<UnsavedChangesWarning />
<WelcomeGuide />
</WidgetAreasBlockEditorProvider>
</ErrorBoundary>
);
}

Expand Down
25 changes: 22 additions & 3 deletions packages/edit-widgets/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
unstable__bootstrapServerSideBlockDefinitions, // eslint-disable-line camelcase
setFreeformContentHandlerName,
} from '@wordpress/blocks';
import { render } from '@wordpress/element';
import { render, unmountComponentAtNode } from '@wordpress/element';
import {
registerCoreBlocks,
__experimentalGetCoreBlocks,
Expand Down Expand Up @@ -36,13 +36,32 @@ const disabledBlocks = [
...( ! ALLOW_REUSABLE_BLOCKS && [ 'core/block' ] ),
];

/**
* Reinitializes the editor after the user chooses to reboot the editor after
* an unhandled error occurs, replacing previously mounted editor element using
* an initial state from prior to the crash.
*
* @param {Element} target DOM node in which editor is rendered.
* @param {?Object} settings Editor settings object.
*/
export function reinitializeEditor( target, settings ) {
unmountComponentAtNode( target );
const reboot = reinitializeEditor.bind( null, target, settings );
render(
<Layout blockEditorSettings={ settings } onError={ reboot } />,
target
);
}

/**
* Initializes the block editor in the widgets screen.
*
* @param {string} id ID of the root element to render the screen in.
* @param {Object} settings Block editor settings.
*/
export function initialize( id, settings ) {
const target = document.getElementById( id );
const reboot = reinitializeEditor.bind( null, target, settings );
const coreBlocks = __experimentalGetCoreBlocks().filter( ( block ) => {
return ! (
disabledBlocks.includes( block.name ) ||
Expand Down Expand Up @@ -70,8 +89,8 @@ export function initialize( id, settings ) {
// see: https://github.com/WordPress/gutenberg/issues/33097
setFreeformContentHandlerName( 'core/html' );
render(
<Layout blockEditorSettings={ settings } />,
document.getElementById( id )
<Layout blockEditorSettings={ settings } onError={ reboot } />,
target
);
}

Expand Down
1 change: 1 addition & 0 deletions packages/edit-widgets/src/style.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@import "../../interface/src/style.scss";

@import "./blocks/widget-area/editor.scss";
@import "./components/error-boundary/style.scss";
@import "./components/header/style.scss";
@import "./components/keyboard-shortcut-help-modal/style.scss";
@import "./components/more-menu/style.scss";
Expand Down