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

Add site editor initial redirect error handling #38655

Merged
merged 6 commits into from
Feb 11, 2022
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
39 changes: 11 additions & 28 deletions packages/edit-site/src/components/error-boundary/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,11 @@
*/
import { Component } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { Button } from '@wordpress/components';
import { Warning } from '@wordpress/block-editor';
import { useCopyToClipboard } from '@wordpress/compose';

function CopyButton( { text, children } ) {
const ref = useCopyToClipboard( text );
return (
<Button variant="secondary" ref={ ref }>
{ children }
</Button>
);
}
/**
* Internal dependencies
*/
import ErrorBoundaryWarning from './warning';

export default class ErrorBoundary extends Component {
constructor() {
Expand Down Expand Up @@ -42,23 +35,13 @@ export default class ErrorBoundary extends Component {
}

return (
<Warning
className="editor-error-boundary"
actions={ [
<Button
key="recovery"
onClick={ this.reboot }
variant="secondary"
>
{ __( 'Attempt Recovery' ) }
</Button>,
<CopyButton key="copy-error" text={ error.stack }>
{ __( 'Copy Error' ) }
</CopyButton>,
] }
>
{ __( 'The editor has encountered an unexpected error.' ) }
</Warning>
<ErrorBoundaryWarning
message={ __(
'The editor has encountered an unexpected error.'
) }
error={ error }
reboot={ this.reboot }
/>
);
}
}
59 changes: 59 additions & 0 deletions packages/edit-site/src/components/error-boundary/warning.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { Button } from '@wordpress/components';
import { Warning } from '@wordpress/block-editor';
import { useCopyToClipboard } from '@wordpress/compose';

function CopyButton( { text, children } ) {
const ref = useCopyToClipboard( text );
return (
<Button variant="secondary" ref={ ref }>
{ children }
</Button>
);
}

export default function ErrorBoundaryWarning( {
message,
error,
reboot,
dashboardLink,
} ) {
const actions = [];

if ( reboot ) {
actions.push(
<Button key="recovery" onClick={ reboot } variant="secondary">
{ __( 'Attempt Recovery' ) }
</Button>
);
}

if ( error ) {
actions.push(
<CopyButton key="copy-error" text={ error.stack }>
Copy link
Member

@noisysocks noisysocks Feb 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we need to add a fallback here. When I add an empty front-page.php to Twenty Twenty-two and open the Site Editor I see an error ("The editor is unable to find a block template for the homepage.") but pressing Copy Error does nothing.

In React DevTools I can see that there is no text.

Screen Shot 2022-02-15 at 12 04 42

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So somewhere in the code isn't passing a proper error object? I can do some debugging, but if you have any clues let me know.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No clues, sorry. It was pretty easy to reproduce, though. Add an empty front-page.php file to the top level of twentytwentytwo.

{ __( 'Copy Error' ) }
</CopyButton>
);
}
talldan marked this conversation as resolved.
Show resolved Hide resolved

if ( dashboardLink ) {
actions.push(
<Button
key="back-to-dashboard"
variant="secondary"
href={ dashboardLink }
>
{ __( 'Back to dashboard' ) }
</Button>
);
}

return (
<Warning className="editor-error-boundary" actions={ actions }>
{ message }
</Warning>
);
}
31 changes: 27 additions & 4 deletions packages/edit-site/src/components/routes/redirect-to-homepage.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,17 @@ function getNeedsHomepageRedirect( params ) {
);
}

/**
* Returns the postType and postId of the default homepage.
*
* @param {string} siteUrl The URL of the site.
* @return {Object} An object containing the postType and postId properties
* or `undefined` if a homepage could not be found.
*/
async function getHomepageParams( siteUrl ) {
const siteSettings = await apiFetch( { path: '/wp/v2/settings' } );
if ( ! siteSettings ) {
return;
throw new Error( '`getHomepageParams`: unable to load site settings.' );
}

const {
Expand All @@ -44,11 +51,27 @@ async function getHomepageParams( siteUrl ) {
// (packages/core-data/src/resolvers.js)
const template = await window
.fetch( addQueryArgs( siteUrl, { '_wp-find-template': true } ) )
.then( ( res ) => res.json() )
.then( ( { data } ) => data );
.then( ( response ) => {
if ( ! response.ok ) {
throw new Error(
`\`getHomepageParams\`: HTTP status error, ${ response.status } ${ response.statusText }`
);
}

return response.json();
} )
.then( ( { data } ) => {
if ( data.message ) {
throw new Error(
`\`getHomepageParams\`: REST API error, ${ data.message }`
);
}

return data;
} );

if ( ! template?.id ) {
return;
throw new Error( '`getHomepageParams`: unable to find home template.' );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Maybe we should make this message more user-friendly and display it instead of a generic error message?

The editor is unable to find a block template for the home page.

What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, sounds good 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I've kept these error messages the same so that we can locate them in the code when the user uses 'Copy Error', but the visible message is now what you suggested.

}

return {
Expand Down
18 changes: 17 additions & 1 deletion packages/edit-site/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
__experimentalFetchUrlData as fetchUrlData,
} from '@wordpress/core-data';
import { store as editorStore } from '@wordpress/editor';
import { __ } from '@wordpress/i18n';
import { store as viewportStore } from '@wordpress/viewport';
import { getQueryArgs } from '@wordpress/url';

Expand All @@ -24,6 +25,7 @@ import { store as editSiteStore } from './store';
import EditSiteApp from './components/app';
import getIsListPage from './utils/get-is-list-page';
import redirectToHomepage from './components/routes/redirect-to-homepage';
import ErrorBoundaryWarning from './components/error-boundary/warning';

/**
* Reinitializes the editor after the user chooses to reboot the editor after
Expand All @@ -38,7 +40,21 @@ export async function reinitializeEditor( target, settings ) {
// define what's being edited. When visiting via the dashboard link, these
// won't be present. Do a client side redirect to the 'homepage' if that's
// the case.
await redirectToHomepage( settings.siteUrl );
try {
await redirectToHomepage( settings.siteUrl );
} catch ( error ) {
render(
<ErrorBoundaryWarning
message={ __(
'The editor is unable to find a block template for the homepage.'
) }
error={ error }
dashboardLink="index.php"
kevin940726 marked this conversation as resolved.
Show resolved Hide resolved
/>,
target
);
return;
}

// This will be a no-op if the target doesn't have any React nodes.
unmountComponentAtNode( target );
Expand Down