Skip to content
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

Framework: Capture and recover from application error #3208

Merged
merged 6 commits into from
Nov 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 12 additions & 5 deletions components/clipboard-button/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,26 @@ class ClipboardButton extends Component {
}

getText() {
return this.props.text;
let text = this.props.text;
if ( 'function' === typeof text ) {
text = text();
}

return text;
}

render() {
const { className, children } = this.props;
// Disable reason: Exclude from spread props passed to Button
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I usually use omit for this. I can follow this approach for consistency though

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ESLint warning is not great, and we've had to disable it elsewhere. I just found this today, and am inclined to enable the option for our configuration:

https://eslint.org/docs/rules/no-unused-vars#ignorerestsiblings

omit is fine too, but means an extra dependency and also duplicating the references (once in destructure, once in omit keys listing).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it wouldn't be a verbatim duplication, since text here is in-fact unused. I see some value in leaving ignoreRestSIblings as the default false behavior, but... it is a convenient omitting technique. Hmm!

// eslint-disable-next-line no-unused-vars
const { className, children, onCopy, text, ...buttonProps } = this.props;
const classes = classnames( 'components-clipboard-button', className );

return (
<div ref={ this.bindContainer }>
<Button className={ classes }>
<span ref={ this.bindContainer }>
<Button { ...buttonProps } className={ classes }>
{ children }
</Button>
</div>
</span>
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion editor/assets/stylesheets/_z-index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ $z-layers: (
'.editor-visual-editor__block .wp-block-more:before': -1,
'.editor-visual-editor__block {core/image aligned left or right}': 20,
'.freeform-toolbar': 10,
'.editor-visual-editor__block-warning': 1,
'.editor-warning': 1,
'.editor-visual-editor__sibling-inserter': 1,
'.components-form-toggle__input': 1,
'.editor-format-list__menu': 1,
Expand Down
76 changes: 76 additions & 0 deletions editor/components/error-boundary/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* External dependencies
*/
import { noop } from 'lodash';

/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { Button, ClipboardButton } from '@wordpress/components';

/**
* Internal dependencies
*/
import { Warning } from '../';
import { getEditedPostContent } from '../../selectors';

class ErrorBoundary extends Component {
constructor() {
super( ...arguments );

this.reboot = this.reboot.bind( this );
this.getContent = this.getContent.bind( this );

this.state = {
error: null,
};
}

componentDidCatch( error ) {
this.setState( { error } );
}

reboot() {
this.props.onError( this.context.store.getState() );
}

getContent() {
try {
return getEditedPostContent( this.context.store.getState() );
} catch ( error ) {}
}

render() {
const { error } = this.state;
if ( ! error ) {
return this.props.children;
}

return (
<Warning>
<p>{ __(
'The editor has encountered an unexpected error.'
) }</p>
<p>
<Button onClick={ this.reboot } isLarge>
{ __( 'Attempt Recovery' ) }
</Button>
<ClipboardButton text={ this.getContent } isLarge>
{ __( 'Copy Post Text' ) }
</ClipboardButton>
<ClipboardButton text={ error.stack } isLarge>
{ __( 'Copy Error' ) }
</ClipboardButton>
</p>
</Warning>
);
}
}

ErrorBoundary.contextTypes = {
store: noop,
};

export default ErrorBoundary;
2 changes: 2 additions & 0 deletions editor/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ export { default as WordCount } from './word-count';

// Content Related Components
export { default as BlockInspector } from './block-inspector';
export { default as ErrorBoundary } from './error-boundary';
export { default as Inserter } from './inserter';
export { default as Warning } from './warning';

// State Related Components
export { default as EditorProvider } from './provider';
9 changes: 7 additions & 2 deletions editor/components/provider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,13 @@ class EditorProvider extends Component {
constructor( props ) {
super( ...arguments );

const store = createReduxStore();
store.dispatch( setupEditor( props.post ) );
const store = createReduxStore( props.initialState );

// If initial state is passed, assume that we don't need to initialize,
// as in the case of an error recovery.
if ( ! props.initialState ) {
store.dispatch( setupEditor( props.post ) );
}

this.store = store;
this.settings = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
*/
import { Dashicon } from '@wordpress/components';

function BlockWarning( { children } ) {
/**
* Internal dependencies
*/
import './style.scss';

function Warning( { children } ) {
return (
<div className="editor-visual-editor__block-warning">
<div className="editor-warning">
<Dashicon icon="warning" />
{ children }
</div>
);
}

export default BlockWarning;
export default Warning;
29 changes: 29 additions & 0 deletions editor/components/warning/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
.editor-warning {
z-index: z-index( '.editor-warning' );
position: absolute;
top: 50%;
left: 50%;
transform: translate( -50%, -50% );
display: flex;
flex-direction: column;
justify-content: space-around;
align-items: center;
width: 96%;
max-width: 780px;
padding: 20px 20px 10px 20px;
background-color: $white;
border: 1px solid $light-gray-500;
text-align: center;
line-height: $default-line-height;
box-shadow: $shadow-popover;

.editor-visual-editor & p {
width: 100%;
font-family: $default-font;
font-size: $default-font-size;
}

.components-button {
margin: 0 #{ $item-spacing / 2 } 5px;
}
}
40 changes: 33 additions & 7 deletions editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import 'moment-timezone/moment-timezone-utils';
/**
* WordPress dependencies
*/
import { render } from '@wordpress/element';
import { render, unmountComponentAtNode } from '@wordpress/element';
import { settings as dateSettings } from '@wordpress/date';

/**
* Internal dependencies
*/
import './assets/stylesheets/main.scss';
import Layout from './layout';
import { EditorProvider } from './components';
import { EditorProvider, ErrorBoundary } from './components';
import { initializeMetaBoxState } from './actions';

export * from './components';
Expand Down Expand Up @@ -45,23 +45,49 @@ window.jQuery( document ).on( 'heartbeat-tick', ( event, response ) => {
}
} );

/**
* 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 {*} initialState Initial editor state to hydrate
*/
export function recreateEditorInstance( target, initialState ) {
unmountComponentAtNode( target );

const reboot = recreateEditorInstance.bind( null, target );

render(
<EditorProvider initialState={ initialState }>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you confirm that we can safely omit „settings” prop here?

Copy link
Member Author

@aduth aduth Nov 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, on second glance, I think this may be needed. I had assumed this was being used to initialize state and therefore would be preserved in the reboot, but in fact this is part of a separate context. Will create a pull request to reintroduce.

<ErrorBoundary onError={ reboot }>
<Layout />
</ErrorBoundary>
</EditorProvider>,
target
);
}

/**
* Initializes and returns an instance of Editor.
*
* The return value of this function is not necessary if we change where we
* call createEditorInstance(). This is due to metaBox timing.
*
* @param {String} id Unique identifier for editor instance
* @param {Object} post API entity for post to edit
* @param {?Object} settings Editor settings object
* @return {Object} Editor interface. Currently supports metabox initialization.
* @param {String} id Unique identifier for editor instance
* @param {Object} post API entity for post to edit
* @param {?Object} settings Editor settings object
* @return {Object} Editor interface
*/
export function createEditorInstance( id, post, settings ) {
const target = document.getElementById( id );
const reboot = recreateEditorInstance.bind( null, target );

const provider = render(
<EditorProvider settings={ settings } post={ post }>
<Layout />
<ErrorBoundary onError={ reboot }>
<Layout />
</ErrorBoundary>
</EditorProvider>,
target
);
Expand Down
8 changes: 4 additions & 4 deletions editor/modes/visual-editor/block-crash-warning.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import BlockWarning from './block-warning';
import { Warning } from '../../components';

const warning = (
<BlockWarning>
<Warning>
<p>{ __(
'This block has suffered from an unhandled error and cannot be previewed.'
'This block has encountered an error and cannot be previewed.'
) }</p>
</BlockWarning>
</Warning>
);

export default () => warning;
6 changes: 3 additions & 3 deletions editor/modes/visual-editor/invalid-block-warning.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Button } from '@wordpress/components';
/**
* Internal dependencies
*/
import BlockWarning from './block-warning';
import { Warning } from '../../components';
import {
getBlockType,
getUnknownTypeHandlerName,
Expand All @@ -31,7 +31,7 @@ function InvalidBlockWarning( { ignoreInvalid, switchToBlockType } ) {
const switchTo = ( blockType ) => () => switchToBlockType( blockType );

return (
<BlockWarning>
<Warning>
<p>{ defaultBlockType && htmlBlockType && sprintf( __(
'This block appears to have been modified externally. ' +
'Overwrite the external changes or Convert to %s or %s to keep ' +
Expand Down Expand Up @@ -67,7 +67,7 @@ function InvalidBlockWarning( { ignoreInvalid, switchToBlockType } ) {
</Button>
) }
</p>
</BlockWarning>
</Warning>
);
}

Expand Down
36 changes: 1 addition & 35 deletions editor/modes/visual-editor/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
position: relative;
min-height: 250px;

> :not( .editor-visual-editor__block-warning ) {
> :not( .editor-warning ) {
pointer-events: none;
user-select: none;
}
Expand Down Expand Up @@ -421,40 +421,6 @@
height: 20px;
}

.editor-visual-editor__block-warning {
z-index: z-index( '.editor-visual-editor__block-warning' );
position: absolute;
top: 50%;
left: 50%;
transform: translate( -50%, -50% );
display: flex;
flex-direction: column;
justify-content: space-around;
align-items: center;
width: 96%;
max-width: 780px;
padding: 20px 20px 10px 20px;
background-color: $white;
border: 1px solid $light-gray-500;
text-align: center;
line-height: $default-line-height;
box-shadow: $shadow-popover;

p {
width: 100%;
font-family: $default-font;
font-size: $default-font-size;
}

.button + .button {
margin-left: $item-spacing;
}
}

.visual-editor__invalid-block-warning-buttons .components-button {
margin-bottom: 5px;
}

.editor-visual-editor__block .blocks-visual-editor__block-html-textarea {
display: block;
margin: 0;
Expand Down
7 changes: 4 additions & 3 deletions editor/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ const GUTENBERG_PREFERENCES_KEY = `GUTENBERG_PREFERENCES_${ window.userSettings.
/**
* Creates a new instance of a Redux store.
*
* @return {Redux.Store} Redux store
* @param {?*} preloadedState Optional initial state
* @return {Redux.Store} Redux store
*/
function createReduxStore() {
function createReduxStore( preloadedState ) {
const enhancers = [
applyMiddleware( multi, refx( effects ) ),
storePersist( 'preferences', GUTENBERG_PREFERENCES_KEY ),
Expand All @@ -33,7 +34,7 @@ function createReduxStore() {
enhancers.push( window.__REDUX_DEVTOOLS_EXTENSION__() );
}

const store = createStore( reducer, flowRight( enhancers ) );
const store = createStore( reducer, preloadedState, flowRight( enhancers ) );

return store;
}
Expand Down
Loading