-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Framework: Capture and recover from application error #3208
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
Changes from 1 commit
3a92956
9c4f8bb
29325a5
bcf9e75
a1d497e
cc658ae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| /** | ||
| * 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 '../'; | ||
|
|
||
| class ErrorBoundary extends Component { | ||
| constructor() { | ||
| super( ...arguments ); | ||
|
|
||
| this.reboot = this.reboot.bind( this ); | ||
| this.getStateText = this.getStateText.bind( this ); | ||
|
|
||
| this.state = { | ||
| hasEncounteredError: false, | ||
| }; | ||
| } | ||
|
|
||
| componentDidCatch() { | ||
| this.setState( { hasEncounteredError: true } ); | ||
| } | ||
|
|
||
| reboot() { | ||
| this.props.onError( this.context.store.getState() ); | ||
| } | ||
|
|
||
| getStateText() { | ||
| return JSON.stringify( this.context.store.getState() ); | ||
| } | ||
|
|
||
| render() { | ||
| const { hasEncounteredError } = this.state; | ||
| if ( ! hasEncounteredError ) { | ||
| 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.getStateText } isLarge> | ||
| { __( 'Copy State to Clipboard' ) } | ||
| </ClipboardButton> | ||
| </p> | ||
| </Warning> | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| ErrorBoundary.contextTypes = { | ||
| store: noop, | ||
| }; | ||
|
|
||
| export default ErrorBoundary; |
| 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; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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'; | ||
|
|
@@ -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 }> | ||
| <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> | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Correct, I would expect we shouldn't need to re-dispatch the action, since the effects of the original initialization should be reflected in the state that is used for rebooting. |
||
| </EditorProvider>, | ||
| target | ||
| ); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 ), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens to the preferences when we use an "initialState" should we ignore hydrating them? |
||
|
|
@@ -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; | ||
| } | ||
|
|
||
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.
Can you confirm that we can safely omit „settings” prop here?
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.
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.