-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
[RNMobile] Add error boundary components and exception logging #59221
Merged
Merged
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
34221bb
Add parse function to send exceptions over the RN bridge
fluiddot 95e9a25
Add RN bridge function to log exceptions to the host app
fluiddot 808d2d9
Add error boundary to blocks
fluiddot 205987e
Add error boundary at editor level
fluiddot 9d57c27
Log exceptions from error boundary components
fluiddot 265b139
Remove `in_app` stack trace parameter
fluiddot ca76b90
Remove `getPopSize`
fluiddot 5d4be1a
Simplify functions from `parseException`
fluiddot 1c9384e
Rename exception tag to `gutenberg_mobile_version`
fluiddot 53b2f2e
Add `gutenbergDidRequestLogException` bridge function to demo app
fluiddot bc67ebb
Trigger callback upon sending JS exception
fluiddot 3e30aaf
Format `RNReactNativeGutenbergBridge`
fluiddot f4bfed0
Update inline comments
fluiddot 37348cb
Merge `reverseEntries` logic into `parseStacktrace`
fluiddot 2a519b4
Set second param of `parseException` (context and tags) as optional
fluiddot 4dece96
Add unit tests of `parseException`
fluiddot 12aff79
Add inline comment to `getReactNativeContext`
fluiddot 5f4e208
Add typing for JavaScript exception in bridge
fluiddot a746556
Fix param type in `gutenbergDidRequestLogException`
fluiddot 35338be
Update `gutenbergDidRequestLogException` implementation of demo app
fluiddot 5457c0e
Rename `JSException` to avoid disambiguation with Crash Logging service
fluiddot 4a6b38b
Add `actions` prop to `Warning` component
fluiddot 5e1daea
Allow extra styles in `Warning` component
fluiddot 260cb3a
Implement copy buttons in Error boundary component
fluiddot a598b77
Fix style import path in error boundary component
fluiddot c017e12
Merge branch 'trunk' into rnmobile/add/error-boundary
fluiddot 7c18727
Change GutenbergJSException `function` param to non-optional
fluiddot 363cb12
Rename JSException param to `message`
fluiddot 294ac99
Rename GutenbergJSException param to `message`
fluiddot 7dadd4a
Merge branch 'trunk' into rnmobile/add/error-boundary
fluiddot 2002dcf
Update `react-native-editor` changelog
fluiddot cfaf638
Update unit tests of `parseException`
fluiddot 49f32e7
[RNMobile] Add error boundry handling for Android (#59385)
jhnstn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
packages/block-editor/src/components/block-list/block-crash-boundary.native.js
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,43 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Component } from '@wordpress/element'; | ||
import { logException } from '@wordpress/react-native-bridge'; | ||
|
||
class BlockCrashBoundary extends Component { | ||
constructor() { | ||
super( ...arguments ); | ||
|
||
this.state = { | ||
error: null, | ||
}; | ||
} | ||
|
||
static getDerivedStateFromError( error ) { | ||
return { error }; | ||
} | ||
|
||
componentDidCatch( error ) { | ||
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. Here's more information about this component function: https://react.dev/reference/react/Component#componentdidcatch |
||
const { blockName } = this.props; | ||
|
||
logException( error, { | ||
context: { | ||
component_stack: error.componentStack, | ||
block_name: blockName, | ||
}, | ||
isHandled: true, | ||
handledBy: 'Block-level Error Boundary', | ||
} ); | ||
} | ||
|
||
render() { | ||
const { error } = this.state; | ||
if ( ! error ) { | ||
return this.props.children; | ||
} | ||
|
||
return this.props.fallback; | ||
} | ||
} | ||
|
||
export default BlockCrashBoundary; |
19 changes: 19 additions & 0 deletions
19
packages/block-editor/src/components/block-list/block-crash-warning.native.js
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,19 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import Warning from '../warning'; | ||
|
||
const warning = ( | ||
<Warning | ||
message={ __( | ||
'This block has encountered an error and cannot be previewed.' | ||
) } | ||
/> | ||
); | ||
|
||
export default () => 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
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
122 changes: 122 additions & 0 deletions
122
packages/editor/src/components/error-boundary/index.native.js
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,122 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { Text, TouchableOpacity, View } from 'react-native'; | ||
import Clipboard from '@react-native-clipboard/clipboard'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Component } from '@wordpress/element'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { select } from '@wordpress/data'; | ||
import { Warning } from '@wordpress/block-editor'; | ||
import { logException } from '@wordpress/react-native-bridge'; | ||
import { usePreferredColorSchemeStyle } from '@wordpress/compose'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { store as editorStore } from '../../store'; | ||
import styles from './style.scss'; | ||
|
||
function getContent() { | ||
try { | ||
// While `select` in a component is generally discouraged, it is | ||
// used here because it (a) reduces the chance of data loss in the | ||
// case of additional errors by performing a direct retrieval and | ||
// (b) avoids the performance cost associated with unnecessary | ||
// content serialization throughout the lifetime of a non-erroring | ||
// application. | ||
return select( editorStore ).getEditedPostContent(); | ||
} catch ( error ) {} | ||
} | ||
|
||
function CopyButton( { text, label, accessibilityLabel, accessibilityHint } ) { | ||
const containerStyle = usePreferredColorSchemeStyle( | ||
styles[ 'copy-button__container' ], | ||
styles[ 'copy-button__container--dark' ] | ||
); | ||
const textStyle = usePreferredColorSchemeStyle( | ||
styles[ 'copy-button__text' ], | ||
styles[ 'copy-button__text--dark' ] | ||
); | ||
|
||
return ( | ||
<TouchableOpacity | ||
activeOpacity={ 0.5 } | ||
accessibilityLabel={ accessibilityLabel } | ||
style={ containerStyle } | ||
accessibilityRole={ 'button' } | ||
accessibilityHint={ accessibilityHint } | ||
onPress={ () => { | ||
Clipboard.setString( | ||
typeof text === 'function' ? text() : text || '' | ||
); | ||
} } | ||
> | ||
<Text style={ textStyle }>{ label }</Text> | ||
</TouchableOpacity> | ||
); | ||
} | ||
|
||
class ErrorBoundary extends Component { | ||
constructor() { | ||
super( ...arguments ); | ||
|
||
this.state = { | ||
error: null, | ||
}; | ||
} | ||
|
||
componentDidCatch( error ) { | ||
logException( error, { | ||
context: { | ||
component_stack: error.componentStack, | ||
}, | ||
isHandled: true, | ||
handledBy: 'Editor-level Error Boundary', | ||
} ); | ||
} | ||
|
||
static getDerivedStateFromError( error ) { | ||
return { error }; | ||
} | ||
|
||
render() { | ||
const { error } = this.state; | ||
if ( ! error ) { | ||
return this.props.children; | ||
} | ||
|
||
const actions = ( | ||
<View style={ styles[ 'error-boundary__actions-container' ] }> | ||
<CopyButton | ||
label={ __( 'Copy Post Text' ) } | ||
accessibilityLabel={ __( 'Button to copy post text' ) } | ||
accessibilityHint={ __( 'Tap here to copy post text' ) } | ||
text={ getContent } | ||
/> | ||
<CopyButton | ||
label={ __( 'Copy Error' ) } | ||
accessibilityLabel={ __( 'Button to copy error' ) } | ||
accessibilityHint={ __( 'Tap here to copy error' ) } | ||
text={ error.stack } | ||
/> | ||
</View> | ||
); | ||
|
||
return ( | ||
<Warning | ||
actions={ actions } | ||
message={ __( | ||
'The editor has encountered an unexpected error.' | ||
) } | ||
containerStyle={ styles[ 'error-boundary__container' ] } | ||
messageStyle={ styles[ 'error-boundary__message' ] } | ||
/> | ||
); | ||
} | ||
} | ||
|
||
export default ErrorBoundary; |
39 changes: 39 additions & 0 deletions
39
packages/editor/src/components/error-boundary/style.native.scss
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,39 @@ | ||
.error-boundary__container { | ||
flex-grow: 1; | ||
justify-content: flex-start; | ||
} | ||
|
||
.error-boundary__message { | ||
font-size: 16; | ||
} | ||
|
||
.error-boundary__actions-container { | ||
margin-top: 16px; | ||
justify-content: center; | ||
flex-direction: row; | ||
} | ||
|
||
.copy-button__container { | ||
background-color: $light-primary; | ||
border-radius: 3px; | ||
padding: $grid-unit $grid-unit-20; | ||
margin-top: 8px; | ||
margin-left: 8px; | ||
margin-right: 8px; | ||
} | ||
|
||
.copy-button__container--dark { | ||
color: $background-dark-secondary; | ||
background-color: $gray-20; | ||
} | ||
|
||
.copy-button__text { | ||
text-align: center; | ||
color: $white; | ||
font-size: 16; | ||
font-weight: 400; | ||
} | ||
|
||
.copy-button__text--dark { | ||
color: $black; | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Here's more information about this static function: https://react.dev/reference/react/Component#static-getderivedstatefromerror