|
1 | 1 | import { Component } from '@wordpress/element'; |
2 | 2 | import { __ } from '@wordpress/i18n'; |
3 | | -import { Button } from '@wordpress/components'; |
4 | | -import { Warning } from '@wordpress/block-editor'; |
| 3 | +// eslint-disable-next-line @wordpress/use-recommended-components -- The fallback UI renders outside the editor's notice system. |
| 4 | +import { Card, CollapsibleCard, Notice, Stack, Text } from '@wordpress/ui'; |
5 | 5 | import { useCopyToClipboard } from '@wordpress/compose'; |
6 | 6 | import { doAction } from '@wordpress/hooks'; |
7 | 7 |
|
8 | | -function CopyButton( { text, children } ) { |
| 8 | +// A boundary catches whatever was thrown, which is not always an `Error`. |
| 9 | +function getErrorName( error ) { |
| 10 | + return ( error instanceof Error && error.name ) || 'Error'; |
| 11 | +} |
| 12 | + |
| 13 | +function getErrorMessage( error ) { |
| 14 | + if ( typeof error === 'string' && error ) { |
| 15 | + return error; |
| 16 | + } |
| 17 | + |
| 18 | + if ( typeof error?.message === 'string' && error.message ) { |
| 19 | + return error.message; |
| 20 | + } |
| 21 | + |
| 22 | + return 'An unknown error occurred.'; |
| 23 | +} |
| 24 | + |
| 25 | +// The sections of the report, shared by the copied Markdown and the details |
| 26 | +// panel so that the two cannot drift apart. Deliberately untranslated: both |
| 27 | +// are developer-facing, and the report is pasted into a bug report. |
| 28 | +function getErrorSections( error, componentStack ) { |
| 29 | + const sections = [ |
| 30 | + { label: getErrorName( error ), content: getErrorMessage( error ) }, |
| 31 | + ]; |
| 32 | + |
| 33 | + if ( error?.stack ) { |
| 34 | + sections.push( { |
| 35 | + label: 'Stack', |
| 36 | + content: error.stack.trim(), |
| 37 | + preformatted: true, |
| 38 | + } ); |
| 39 | + } |
| 40 | + |
| 41 | + if ( componentStack ) { |
| 42 | + sections.push( { |
| 43 | + label: 'Component stack', |
| 44 | + content: componentStack.trim(), |
| 45 | + preformatted: true, |
| 46 | + } ); |
| 47 | + } |
| 48 | + |
| 49 | + sections.push( { |
| 50 | + label: 'Environment', |
| 51 | + content: `User agent: ${ window.navigator.userAgent }`, |
| 52 | + preformatted: true, |
| 53 | + } ); |
| 54 | + |
| 55 | + return sections; |
| 56 | +} |
| 57 | + |
| 58 | +// Markdown, so the report stays readable as plain text and renders when pasted |
| 59 | +// into a bug report. |
| 60 | +function getErrorReport( error, componentStack ) { |
| 61 | + const sections = getErrorSections( error, componentStack ).map( |
| 62 | + ( { label, content, preformatted } ) => |
| 63 | + `**${ label }**\n\n${ |
| 64 | + preformatted ? `\`\`\`\n${ content }\n\`\`\`` : content |
| 65 | + }` |
| 66 | + ); |
| 67 | + |
| 68 | + return [ '### Error report', ...sections ].join( '\n\n' ); |
| 69 | +} |
| 70 | + |
| 71 | +function CopyButton( { text, children, variant = 'outline' } ) { |
9 | 72 | const ref = useCopyToClipboard( text ); |
10 | 73 | return ( |
11 | | - <Button size="compact" variant="secondary" ref={ ref }> |
| 74 | + <Notice.ActionButton variant={ variant } ref={ ref }> |
12 | 75 | { children } |
13 | | - </Button> |
| 76 | + </Notice.ActionButton> |
| 77 | + ); |
| 78 | +} |
| 79 | + |
| 80 | +function ErrorReport( { error, componentStack } ) { |
| 81 | + return ( |
| 82 | + <Stack |
| 83 | + className="customize-widgets-error-boundary__report" |
| 84 | + direction="column" |
| 85 | + gap="md" |
| 86 | + > |
| 87 | + { getErrorSections( error, componentStack ).map( |
| 88 | + ( { label, content } ) => ( |
| 89 | + <Stack key={ label } direction="column" gap="xs"> |
| 90 | + <Text variant="heading-md">{ label }</Text> |
| 91 | + <pre className="customize-widgets-error-boundary__report-section"> |
| 92 | + { content } |
| 93 | + </pre> |
| 94 | + </Stack> |
| 95 | + ) |
| 96 | + ) } |
| 97 | + </Stack> |
| 98 | + ); |
| 99 | +} |
| 100 | + |
| 101 | +function ErrorDetails( { error, componentStack } ) { |
| 102 | + return ( |
| 103 | + <CollapsibleCard.Root className="customize-widgets-error-boundary__details"> |
| 104 | + <CollapsibleCard.Header> |
| 105 | + <Card.Title>{ __( 'Error details' ) }</Card.Title> |
| 106 | + </CollapsibleCard.Header> |
| 107 | + <CollapsibleCard.Content> |
| 108 | + <ErrorReport |
| 109 | + error={ error } |
| 110 | + componentStack={ componentStack } |
| 111 | + /> |
| 112 | + </CollapsibleCard.Content> |
| 113 | + </CollapsibleCard.Root> |
14 | 114 | ); |
15 | 115 | } |
16 | 116 |
|
17 | 117 | export default class ErrorBoundary extends Component { |
18 | 118 | constructor() { |
19 | 119 | super( ...arguments ); |
| 120 | + |
20 | 121 | this.state = { |
21 | 122 | error: null, |
| 123 | + componentStack: null, |
22 | 124 | }; |
23 | 125 | } |
24 | 126 |
|
25 | | - componentDidCatch( error ) { |
26 | | - this.setState( { error } ); |
| 127 | + componentDidCatch( error, errorInfo ) { |
| 128 | + this.setState( { componentStack: errorInfo?.componentStack } ); |
| 129 | + doAction( 'editor.ErrorBoundary.errorLogged', error, errorInfo ); |
| 130 | + } |
27 | 131 |
|
28 | | - doAction( 'editor.ErrorBoundary.errorLogged', error ); |
| 132 | + static getDerivedStateFromError( error ) { |
| 133 | + return { error }; |
29 | 134 | } |
30 | 135 |
|
31 | 136 | render() { |
32 | | - const { error } = this.state; |
| 137 | + const { error, componentStack } = this.state; |
33 | 138 | if ( ! error ) { |
34 | 139 | return this.props.children; |
35 | 140 | } |
36 | 141 |
|
37 | 142 | return ( |
38 | | - <Warning |
| 143 | + <Stack |
39 | 144 | className="customize-widgets-error-boundary" |
40 | | - actions={ [ |
41 | | - <CopyButton key="copy-error" text={ error.stack }> |
42 | | - { __( 'Copy Error' ) } |
43 | | - </CopyButton>, |
44 | | - ] } |
| 145 | + direction="column" |
| 146 | + gap="lg" |
45 | 147 | > |
46 | | - { __( 'The editor has encountered an unexpected error.' ) } |
47 | | - </Warning> |
| 148 | + <Notice.Root intent="error"> |
| 149 | + <Notice.Title> |
| 150 | + { __( 'The editor has crashed' ) } |
| 151 | + </Notice.Title> |
| 152 | + <Notice.Description> |
| 153 | + { __( |
| 154 | + 'An unknown error occurred. Reload your browser to try again, or copy the error to report the problem or search.' |
| 155 | + ) } |
| 156 | + </Notice.Description> |
| 157 | + <Notice.Actions> |
| 158 | + <CopyButton |
| 159 | + variant="solid" |
| 160 | + text={ () => |
| 161 | + getErrorReport( error, componentStack ) |
| 162 | + } |
| 163 | + > |
| 164 | + { __( 'Copy error' ) } |
| 165 | + </CopyButton> |
| 166 | + </Notice.Actions> |
| 167 | + </Notice.Root> |
| 168 | + { globalThis.SCRIPT_DEBUG ? ( |
| 169 | + <ErrorDetails |
| 170 | + error={ error } |
| 171 | + componentStack={ componentStack } |
| 172 | + /> |
| 173 | + ) : null } |
| 174 | + </Stack> |
48 | 175 | ); |
49 | 176 | } |
50 | 177 | } |
0 commit comments