Skip to content
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

Site Editor: Wrap each router area in 'ErrorBoundary' #64245

Merged
merged 7 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/edit-post/src/components/layout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ function Layout( {

return (
<SlotFillProvider>
<ErrorBoundary>
<ErrorBoundary canCopyContent>
<CommandMenu />
<WelcomeGuide postType={ currentPostType } />
<div
Expand Down
44 changes: 0 additions & 44 deletions packages/edit-site/src/components/error-boundary/index.js

This file was deleted.

This file was deleted.

30 changes: 0 additions & 30 deletions packages/edit-site/src/components/error-boundary/warning.js

This file was deleted.

12 changes: 7 additions & 5 deletions packages/edit-site/src/components/layout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor';
import {
EditorSnackbars,
UnsavedChangesWarning,
ErrorBoundary,
privateApis as editorPrivateApis,
} from '@wordpress/editor';
import { privateApis as coreCommandsPrivateApis } from '@wordpress/core-commands';
Expand All @@ -36,7 +37,6 @@ import { useDispatch } from '@wordpress/data';
/**
* Internal dependencies
*/
import ErrorBoundary from '../error-boundary';
import { default as SiteHub, SiteHubMobile } from '../site-hub';
import ResizableFrame from '../resizable-frame';
import { unlock } from '../../lock-unlock';
Expand Down Expand Up @@ -136,7 +136,9 @@ function Layout() {
}
routeKey={ routeKey }
>
{ areas.sidebar }
<ErrorBoundary>
{ areas.sidebar }
</ErrorBoundary>
</SidebarContent>
<SaveHub />
<SavePanel />
Expand All @@ -160,7 +162,7 @@ function Layout() {
/>
</SidebarContent>
) }
{ areas.mobile }
<ErrorBoundary>{ areas.mobile }</ErrorBoundary>
</div>
) }

Expand All @@ -173,7 +175,7 @@ function Layout() {
maxWidth: widths?.content,
} }
>
{ areas.content }
<ErrorBoundary>{ areas.content }</ErrorBoundary>
</div>
) }

Expand All @@ -184,7 +186,7 @@ function Layout() {
maxWidth: widths?.edit,
} }
>
{ areas.edit }
<ErrorBoundary>{ areas.edit }</ErrorBoundary>
</div>
) }

Expand Down
46 changes: 30 additions & 16 deletions packages/editor/src/components/error-boundary/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
*/
import { Component } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { Button } from '@wordpress/components';
import {
Button,
__experimentalHStack as HStack,
__experimentalText as Text,
} from '@wordpress/components';
import { select } from '@wordpress/data';
import { Warning } from '@wordpress/block-editor';
import { useCopyToClipboard } from '@wordpress/compose';
import { doAction } from '@wordpress/hooks';

Expand All @@ -26,10 +29,10 @@ function getContent() {
} catch ( error ) {}
}

function CopyButton( { text, children } ) {
function CopyButton( { text, children, variant = 'secondary' } ) {
const ref = useCopyToClipboard( text );
return (
<Button __next40pxDefaultSize variant="secondary" ref={ ref }>
<Button __next40pxDefaultSize variant={ variant } ref={ ref }>
{ children }
</Button>
);
Expand All @@ -54,23 +57,34 @@ class ErrorBoundary extends Component {

render() {
const { error } = this.state;
const { canCopyContent = false } = this.props;
if ( ! error ) {
return this.props.children;
}

const actions = [
<CopyButton key="copy-post" text={ getContent }>
{ __( 'Copy Post Text' ) }
</CopyButton>,
<CopyButton key="copy-error" text={ error.stack }>
{ __( 'Copy Error' ) }
</CopyButton>,
];

return (
<Warning className="editor-error-boundary" actions={ actions }>
{ __( 'The editor has encountered an unexpected error.' ) }
</Warning>
<HStack
className="editor-error-boundary"
alignment="baseline"
spacing={ 4 }
justify="space-between"
expanded={ false }
wrap
>
<Text as="p">
{ __( 'The editor has encountered an unexpected error.' ) }
</Text>
<HStack expanded={ false }>
{ canCopyContent && (
<CopyButton text={ getContent }>
{ __( 'Copy contents' ) }
</CopyButton>
) }
<CopyButton variant="primary" text={ error?.stack }>
{ __( 'Copy error' ) }
</CopyButton>
</HStack>
</HStack>
);
}
}
Expand Down
6 changes: 5 additions & 1 deletion packages/editor/src/components/error-boundary/style.scss
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
.editor-error-boundary {
font-family: $default-font;
margin: auto;
max-width: 780px;
padding: 20px;
padding: 1em;
margin-top: 60px;
box-shadow: $elevation-large;
border: $border-width solid $gray-900;
border-radius: $radius-small;
background-color: $white;
}
Loading