-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Changes from all commits
3a92956
9c4f8bb
29325a5
bcf9e75
a1d497e
cc658ae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; |
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 }> | ||
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. Can you confirm that we can safely omit „settings” prop here? 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. 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 | ||
); | ||
|
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 usually use
omit
for this. I can follow this approach for consistency thoughThere 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 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).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 guess it wouldn't be a verbatim duplication, since
text
here is in-fact unused. I see some value in leavingignoreRestSIblings
as the defaultfalse
behavior, but... it is a convenient omitting technique. Hmm!