Repository navigation
Editor: Surface debugging details in the ErrorBoundary #81642
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
fa212e9
Editor: Surface debugging details in the ErrorBoundary
Mamaduka 210f683
Use Notice component
Mamaduka 7ca70af
Update error details styles
Mamaduka 64870ea
Try different colors
Mamaduka 09833db
Feedback
Mamaduka fac48bc
Cleanup
Mamaduka 3f5a6d9
New design
Mamaduka 5d0ea1f
Swap section order
Mamaduka 80a0c40
Use inherited text styling
Mamaduka 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
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| import { Component } from '@wordpress/element'; | ||
| import { __ } from '@wordpress/i18n'; | ||
| import { Button } from '@wordpress/components'; | ||
| import { Stack, Text } from '@wordpress/ui'; | ||
| // eslint-disable-next-line @wordpress/use-recommended-components -- The fallback UI renders outside the editor's notice system. | ||
| import { Card, CollapsibleCard, Notice, Stack, Text } from '@wordpress/ui'; | ||
| import { select } from '@wordpress/data'; | ||
| import { useCopyToClipboard } from '@wordpress/compose'; | ||
| import { doAction } from '@wordpress/hooks'; | ||
|
|
@@ -19,12 +19,112 @@ function getContent() { | |
| } catch {} | ||
| } | ||
|
|
||
| function CopyButton( { text, children, variant = 'secondary' } ) { | ||
| // A boundary catches whatever was thrown, which is not always an `Error`. | ||
| function getErrorName( error ) { | ||
| return ( error instanceof Error && error.name ) || 'Error'; | ||
| } | ||
|
|
||
| function getErrorMessage( error ) { | ||
| if ( typeof error === 'string' && error ) { | ||
| return error; | ||
| } | ||
|
|
||
| if ( typeof error?.message === 'string' && error.message ) { | ||
| return error.message; | ||
| } | ||
|
|
||
| return 'An unknown error occurred.'; | ||
| } | ||
|
|
||
| // The sections of the report, shared by the copied Markdown and the details | ||
| // panel so that the two cannot drift apart. Deliberately untranslated: both | ||
| // are developer-facing, and the report is pasted into a bug report. | ||
| function getErrorSections( error, componentStack ) { | ||
| const sections = [ | ||
| { label: getErrorName( error ), content: getErrorMessage( error ) }, | ||
| ]; | ||
|
|
||
| if ( componentStack ) { | ||
| sections.push( { | ||
| label: 'Component stack', | ||
| content: componentStack.trim(), | ||
| preformatted: true, | ||
| } ); | ||
| } | ||
|
|
||
| if ( error?.stack ) { | ||
| sections.push( { | ||
| label: 'Stack', | ||
| content: error.stack.trim(), | ||
| preformatted: true, | ||
| } ); | ||
| } | ||
|
|
||
| sections.push( { | ||
| label: 'Environment', | ||
| content: `User agent: ${ window.navigator.userAgent }`, | ||
| preformatted: true, | ||
| } ); | ||
|
|
||
| return sections; | ||
| } | ||
|
|
||
| // Markdown, so the report stays readable as plain text and renders when pasted | ||
| // into a bug report. | ||
| function getErrorReport( error, componentStack ) { | ||
| const sections = getErrorSections( error, componentStack ).map( | ||
| ( { label, content, preformatted } ) => | ||
| `**${ label }**\n\n${ | ||
| preformatted ? `\`\`\`\n${ content }\n\`\`\`` : content | ||
| }` | ||
| ); | ||
|
|
||
| return [ '### Error report', ...sections ].join( '\n\n' ); | ||
| } | ||
|
|
||
| function CopyButton( { text, children, variant = 'outline' } ) { | ||
| const ref = useCopyToClipboard( text ); | ||
| return ( | ||
| <Button __next40pxDefaultSize variant={ variant } ref={ ref }> | ||
| <Notice.ActionButton variant={ variant } ref={ ref }> | ||
| { children } | ||
| </Button> | ||
| </Notice.ActionButton> | ||
| ); | ||
| } | ||
|
|
||
| function ErrorReport( { error, componentStack } ) { | ||
| return ( | ||
| <Stack | ||
| className="editor-error-boundary__report" | ||
| direction="column" | ||
| gap="md" | ||
| > | ||
| { getErrorSections( error, componentStack ).map( | ||
| ( { label, content } ) => ( | ||
| <Stack key={ label } direction="column" gap="xs"> | ||
| <Text variant="heading-md">{ label }</Text> | ||
| <pre className="editor-error-boundary__report-section"> | ||
| { content } | ||
| </pre> | ||
| </Stack> | ||
| ) | ||
| ) } | ||
| </Stack> | ||
| ); | ||
| } | ||
|
|
||
| function ErrorDetails( { error, componentStack } ) { | ||
| return ( | ||
| <CollapsibleCard.Root className="editor-error-boundary__details"> | ||
| <CollapsibleCard.Header> | ||
| <Card.Title>{ __( 'Error details' ) }</Card.Title> | ||
| </CollapsibleCard.Header> | ||
| <CollapsibleCard.Content> | ||
| <ErrorReport | ||
| error={ error } | ||
| componentStack={ componentStack } | ||
| /> | ||
| </CollapsibleCard.Content> | ||
| </CollapsibleCard.Root> | ||
| ); | ||
| } | ||
|
|
||
|
|
@@ -34,19 +134,21 @@ class ErrorBoundary extends Component { | |
|
|
||
| this.state = { | ||
| error: null, | ||
| componentStack: null, | ||
| }; | ||
| } | ||
|
|
||
| componentDidCatch( error ) { | ||
| doAction( 'editor.ErrorBoundary.errorLogged', error ); | ||
| componentDidCatch( error, errorInfo ) { | ||
| this.setState( { componentStack: errorInfo?.componentStack } ); | ||
| doAction( 'editor.ErrorBoundary.errorLogged', error, errorInfo ); | ||
| } | ||
|
|
||
| static getDerivedStateFromError( error ) { | ||
| return { error }; | ||
| } | ||
|
|
||
| render() { | ||
| const { error } = this.state; | ||
| const { error, componentStack } = this.state; | ||
| const { canCopyContent = false } = this.props; | ||
| if ( ! error ) { | ||
| return this.props.children; | ||
|
|
@@ -55,24 +157,40 @@ class ErrorBoundary extends Component { | |
| return ( | ||
| <Stack | ||
| className="editor-error-boundary" | ||
| align="baseline" | ||
| direction="column" | ||
| gap="lg" | ||
| justify="space-between" | ||
| wrap="wrap" | ||
| > | ||
| <Text render={ <p /> }> | ||
| { __( 'The editor has encountered an unexpected error.' ) } | ||
| </Text> | ||
| <Stack align="center" gap="sm"> | ||
| { canCopyContent && ( | ||
| <CopyButton text={ getContent }> | ||
| { __( 'Copy contents' ) } | ||
| <Notice.Root intent="error"> | ||
| <Notice.Title> | ||
| { __( 'The editor has crashed' ) } | ||
| </Notice.Title> | ||
| <Notice.Description> | ||
| { __( | ||
| 'An unknown error occurred. Reload your browser to try again, or copy the error to report the problem or search.' | ||
| ) } | ||
| </Notice.Description> | ||
| <Notice.Actions> | ||
| { canCopyContent && ( | ||
| <CopyButton text={ getContent }> | ||
| { __( 'Copy contents' ) } | ||
|
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. Wondering if this could be confused with "contents" of the error. Maybe it could be explicit, e.g., "Copy post content" or "editor content". Or maybe we could tweak the description to explain the option. 🤷🏻 |
||
| </CopyButton> | ||
| ) } | ||
| <CopyButton | ||
| variant="solid" | ||
| text={ () => | ||
| getErrorReport( error, componentStack ) | ||
| } | ||
| > | ||
| { __( 'Copy error' ) } | ||
| </CopyButton> | ||
| ) } | ||
| <CopyButton variant="primary" text={ error?.stack }> | ||
| { __( 'Copy error' ) } | ||
| </CopyButton> | ||
| </Stack> | ||
| </Notice.Actions> | ||
| </Notice.Root> | ||
| { globalThis.SCRIPT_DEBUG ? ( | ||
| <ErrorDetails | ||
| error={ error } | ||
| componentStack={ componentStack } | ||
| /> | ||
| ) : null } | ||
| </Stack> | ||
| ); | ||
| } | ||
|
|
||
This file contains hidden or 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 |
|---|---|---|
| @@ -1,14 +1,26 @@ | ||
| @use "@wordpress/base-styles/colors" as *; | ||
| @use "@wordpress/base-styles/variables" as *; | ||
|
|
||
| .editor-error-boundary { | ||
| font-family: $default-font; | ||
| font-family: var(--wpds-typography-font-family-body); | ||
| margin: auto; | ||
| max-width: 780px; | ||
| padding: 1em; | ||
| width: 100%; | ||
| max-width: var(--wpds-dimension-surface-width-lg); | ||
| padding: var(--wpds-dimension-padding-xl); | ||
| margin-top: $header-height; | ||
| box-shadow: $elevation-large; | ||
| border: $border-width solid $gray-900; | ||
| border-radius: $radius-small; | ||
| background-color: $white; | ||
| } | ||
|
|
||
| .editor-error-boundary__report { | ||
| max-height: 420px; | ||
| overflow: auto; | ||
| overflow-anchor: none; | ||
| color: var(--wpds-color-foreground-content-neutral-weak); | ||
| } | ||
|
|
||
| .editor-error-boundary__report-section { | ||
| margin: 0; | ||
| font-family: var(--wpds-typography-font-family-mono); | ||
| font-size: var(--wpds-typography-font-size-sm); | ||
| line-height: var(--wpds-typography-line-height-sm); | ||
| white-space: pre-wrap; | ||
| overflow-wrap: break-word; | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.