Skip to content

Commit 9ada830

Browse files
t-hamanoclaude
andcommitted
Widgets: Surface debugging details in the ErrorBoundary
Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 52b3bec commit 9ada830

9 files changed

Lines changed: 333 additions & 47 deletions

File tree

‎packages/customize-widgets/CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### Enhancements
6+
7+
- Error boundary: Surface debugging details ([#{TODO}](https://github.com/WordPress/gutenberg/pull/{TODO})).
8+
59
## 5.54.0 (2026-08-26)
610

711
## 5.53.0 (2026-08-12)
Lines changed: 144 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,177 @@
11
import { Component } from '@wordpress/element';
22
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';
55
import { useCopyToClipboard } from '@wordpress/compose';
66
import { doAction } from '@wordpress/hooks';
77

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' } ) {
972
const ref = useCopyToClipboard( text );
1073
return (
11-
<Button size="compact" variant="secondary" ref={ ref }>
74+
<Notice.ActionButton variant={ variant } ref={ ref }>
1275
{ 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>
14114
);
15115
}
16116

17117
export default class ErrorBoundary extends Component {
18118
constructor() {
19119
super( ...arguments );
120+
20121
this.state = {
21122
error: null,
123+
componentStack: null,
22124
};
23125
}
24126

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+
}
27131

28-
doAction( 'editor.ErrorBoundary.errorLogged', error );
132+
static getDerivedStateFromError( error ) {
133+
return { error };
29134
}
30135

31136
render() {
32-
const { error } = this.state;
137+
const { error, componentStack } = this.state;
33138
if ( ! error ) {
34139
return this.props.children;
35140
}
36141

37142
return (
38-
<Warning
143+
<Stack
39144
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"
45147
>
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>
48175
);
49176
}
50177
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
.customize-widgets-error-boundary {
2+
font-family: var(--wpds-typography-font-family-body);
3+
padding: var(--wpds-dimension-padding-md);
4+
}
5+
6+
.customize-widgets-error-boundary__report {
7+
max-height: 420px;
8+
overflow: auto;
9+
overflow-anchor: none;
10+
}
11+
12+
.customize-widgets-error-boundary__report-section {
13+
margin: 0;
14+
white-space: pre-wrap;
15+
overflow-wrap: break-word;
16+
}

‎packages/customize-widgets/src/components/error-boundary/test/error-boundary.js‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const ChildComponent = () => {
1010

1111
describe( 'Error Boundary', () => {
1212
describe( 'when error is thrown from a Child component', () => {
13-
it( 'calls the `editor.ErrorBoundary.errorLogged` hook action with the error object', () => {
13+
it( 'calls the `editor.ErrorBoundary.errorLogged` hook action with the error object and error info', () => {
1414
const doAction = jest.spyOn( wpHooks, 'doAction' );
1515

1616
render(
@@ -21,7 +21,10 @@ describe( 'Error Boundary', () => {
2121

2222
expect( doAction ).toHaveBeenCalledWith(
2323
'editor.ErrorBoundary.errorLogged',
24-
theError
24+
theError,
25+
expect.objectContaining( {
26+
componentStack: expect.any( String ),
27+
} )
2528
);
2629
expect( console ).toHaveErrored();
2730
} );

‎packages/customize-widgets/src/style.scss‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
@use "@wordpress/base-styles/mixins" as *;
22
@use "@wordpress/base-styles/default-custom-properties";
33
@use "./components/block-inspector-button/style.scss" as *;
4+
@use "./components/error-boundary/style.scss" as *;
45
@use "./components/header/style.scss" as *;
56
@use "./components/inserter/style.scss" as *;
67
@use "./components/keyboard-shortcut-help-modal/style.scss" as *;

‎packages/edit-widgets/CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### Enhancements
6+
7+
- Error boundary: Surface debugging details ([#{TODO}](https://github.com/WordPress/gutenberg/pull/{TODO})).
8+
59
### Bug Fixes
610

711
- Prevent an error in `canInsertBlockInWidgetArea` when no widget area blocks are present.

0 commit comments

Comments
 (0)