Skip to content
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
4 changes: 4 additions & 0 deletions packages/core-data/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Enhancements

- Improve error reporting in private action `saveDirtyEntities` ([#81151](https://github.com/WordPress/gutenberg/pull/81151)).

### Bug Fixes

- Ensure revision resolvers finish after fetched revisions are stored.
Expand Down
71 changes: 63 additions & 8 deletions packages/core-data/src/private-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import apiFetch from '@wordpress/api-fetch';
import { store as noticesStore } from '@wordpress/notices';
import { store as blockEditorStore } from '@wordpress/block-editor';
import { decodeEntities } from '@wordpress/html-entities';
import { __ } from '@wordpress/i18n';

/**
Expand Down Expand Up @@ -313,7 +314,10 @@ export const saveDirtyEntities =
pendingSavedRecords.push(
registry
.dispatch( STORE_NAME )
.saveEditedEntityRecord( kind, name, key )
.saveEditedEntityRecord( kind, name, key, {
throwOnError: true,
} )
.catch( ensureError )
);
}
} );
Expand All @@ -325,28 +329,43 @@ export const saveDirtyEntities =
'root',
'site',
undefined,
siteItemsToSave
siteItemsToSave,
{
throwOnError: true,
}
)
.catch( ensureError )
);
}
registry
.dispatch( blockEditorStore )
.__unstableMarkLastChangeAsPersistent();

Promise.all( pendingSavedRecords )
return Promise.all( pendingSavedRecords )

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why return here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

To allow the whole action to be waited on. This is used in the unit tests, but it seems useful in general.

.then( async ( values ) => {
if ( onSave ) {
await onSave();
}
return values;
} )
.then( ( values ) => {
if (
values.some( ( value ) => typeof value === 'undefined' )
) {
const errors = values.filter( ( v ) => v instanceof Error );
if ( errors.length ) {
const firstMessage = errors.find(
( e ) => e.message
)?.message;

registry
.dispatch( noticesStore )
.createErrorNotice( __( 'Saving failed.' ) );
.createErrorNotice(
decodeEntities(
firstMessage || __( 'Saving failed.' )
),
{
type: 'snackbar',
id: saveNoticeId,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should we pass explicitDismiss: true here? It feels like an error could hide before a user had time to properly see/understand it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Seems reasonable, but I wanted to minimise the changes brought by this PR.

}
);
} else {
registry
.dispatch( noticesStore )
Expand All @@ -370,7 +389,43 @@ export const saveDirtyEntities =
registry
.dispatch( noticesStore )
.createErrorNotice(
`${ __( 'Saving failed.' ) } ${ error }`
decodeEntities(
error?.message || __( 'Saving failed.' )
),
{
type: 'snackbar',
id: saveNoticeId,
}
)
);

function ensureError( error ) {
if ( error instanceof Error ) {
return error;
}

// Expect certain errors to be plain objects with a `message`
// property, such as those thrown by `apiFetch`. Otherwise, do our
// best to infer a message via duck typing.
let message;
if ( ! error ) {
} else if ( typeof error.message === 'string' ) {
message = error.message;
} else if ( typeof error === 'string' ) {
message = error;
} else if (
// Only consider own method, lest we erroneously end up calling
// `Object#toString` at the end of the prototype chain, thereby
// returning `"[object Object]"`.
Object.hasOwn( error, 'toString' ) &&

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Object.hasOwn() will return false for class methods defined on the prototype. Could be yet another rare gap we might want to fill.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I was going to say "good point", started wondering why I had written this in, then I realised: yeah, this is intentional. The fact that Object.prototype.toString exists means that, effectively, any object that doesn't define its own method would render here as "[object Object]". I've pushed a comment to make it absolutely clear, but the unit tests were already catching it. :)

typeof error.toString === 'function'
) {
const result = error.toString();
if ( typeof result === 'string' ) {
message = result;
}
}

return new Error( message, { cause: error } );
}
};
Loading
Loading