Repository navigation
Core Data: saveDirtyEntities: improve messaging of server errors #81151
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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'; | ||
|
|
||
| /** | ||
|
|
@@ -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 ) | ||
| ); | ||
| } | ||
| } ); | ||
|
|
@@ -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 ) | ||
| .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, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we pass
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ) | ||
|
|
@@ -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' ) && | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| typeof error.toString === 'function' | ||
| ) { | ||
| const result = error.toString(); | ||
| if ( typeof result === 'string' ) { | ||
| message = result; | ||
| } | ||
| } | ||
|
|
||
| return new Error( message, { cause: error } ); | ||
| } | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why return here?
There was a problem hiding this comment.
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.