-
Notifications
You must be signed in to change notification settings - Fork 4.7k
[core-data] throwOnError option for saveEntityRecord and deleteEntityRecord actions #39258
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 1 commit
5852b89
8f31d65
241fa50
e58f632
0ffd8da
90eac4a
a361949
2d63a50
5b7b6e8
26530df
717fa02
889acd9
245ed3a
9f847af
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -209,22 +209,26 @@ export function receiveEmbedPreview( url, preview ) { | |
| /** | ||
| * Action triggered to delete an entity record. | ||
| * | ||
| * @param {string} kind Kind of the deleted entity record. | ||
| * @param {string} name Name of the deleted entity record. | ||
| * @param {string} recordId Record ID of the deleted entity record. | ||
| * @param {?Object} query Special query parameters for the | ||
| * DELETE API call. | ||
| * @param {Object} [options] Delete options. | ||
| * @param {Function} [options.__unstableFetch] Internal use only. Function to | ||
| * call instead of `apiFetch()`. | ||
| * Must return a promise. | ||
| * @param {string} kind Kind of the deleted entity. | ||
| * @param {string} name Name of the deleted entity. | ||
| * @param {string} recordId Record ID of the deleted entity. | ||
| * @param {?Object} query Special query parameters for the | ||
| * DELETE API call. | ||
| * @param {Object} [options] Delete options. | ||
| * @param {Function} [options.__unstableFetch] Internal use only. Function to | ||
| * call instead of `apiFetch()`. | ||
| * Must return a promise. | ||
| * @param {boolean} [options.throwOnError=false] Whether to re-throw exceptions. | ||
| * If false, dispatching this action | ||
| * always resolves successfully and. | ||
adamziel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * never rejects. | ||
| */ | ||
| export const deleteEntityRecord = ( | ||
| kind, | ||
| name, | ||
| recordId, | ||
| query, | ||
| { __unstableFetch = apiFetch } = {} | ||
| { __unstableFetch = apiFetch, throwOnError = false } = {} | ||
| ) => async ( { dispatch } ) => { | ||
| const configs = await dispatch( getOrLoadEntitiesConfig( kind ) ); | ||
| const entityConfig = find( configs, { kind, name } ); | ||
|
|
@@ -273,6 +277,10 @@ export const deleteEntityRecord = ( | |
| error, | ||
| } ); | ||
|
|
||
| if ( error && throwOnError ) { | ||
|
||
| throw error; | ||
| } | ||
|
|
||
| return deletedRecord; | ||
| } finally { | ||
| dispatch.__unstableReleaseStoreLock( lock ); | ||
|
|
@@ -386,20 +394,28 @@ export function __unstableCreateUndoLevel() { | |
| /** | ||
| * Action triggered to save an entity record. | ||
| * | ||
| * @param {string} kind Kind of the received entity. | ||
| * @param {string} name Name of the received entity. | ||
| * @param {Object} record Record to be saved. | ||
| * @param {Object} options Saving options. | ||
| * @param {boolean} [options.isAutosave=false] Whether this is an autosave. | ||
| * @param {Function} [options.__unstableFetch] Internal use only. Function to | ||
| * call instead of `apiFetch()`. | ||
| * Must return a promise. | ||
| * @param {string} kind Kind of the received entity. | ||
| * @param {string} name Name of the received entity. | ||
| * @param {Object} record Record to be saved. | ||
| * @param {Object} options Saving options. | ||
| * @param {boolean} [options.isAutosave=false] Whether this is an autosave. | ||
| * @param {Function} [options.__unstableFetch] Internal use only. Function to | ||
| * call instead of `apiFetch()`. | ||
| * Must return a promise. | ||
| * @param {boolean} [options.throwOnError=false] Whether to re-throw exceptions. | ||
| * If false, dispatching this action | ||
| * always resolves successfully and. | ||
adamziel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * never rejects. | ||
| */ | ||
| export const saveEntityRecord = ( | ||
| kind, | ||
| name, | ||
| record, | ||
| { isAutosave = false, __unstableFetch = apiFetch } = {} | ||
| { | ||
| isAutosave = false, | ||
| __unstableFetch = apiFetch, | ||
| throwOnError = false, | ||
| } = {} | ||
| ) => async ( { select, resolveSelect, dispatch } ) => { | ||
| const configs = await dispatch( getOrLoadEntitiesConfig( kind ) ); | ||
| const entityConfig = find( configs, { kind, name } ); | ||
|
|
@@ -578,6 +594,10 @@ export const saveEntityRecord = ( | |
| isAutosave, | ||
| } ); | ||
|
|
||
| if ( error && throwOnError ) { | ||
| throw error; | ||
| } | ||
|
|
||
| return updatedRecord; | ||
| } finally { | ||
| dispatch.__unstableReleaseStoreLock( lock ); | ||
|
|
@@ -759,69 +779,3 @@ export function receiveAutosaves( postId, autosaves ) { | |
| autosaves: castArray( autosaves ), | ||
| }; | ||
| } | ||
|
|
||
| export const throwingSaveEntityRecord = ( ...args ) => async ( { | ||
| dispatch, | ||
| select, | ||
| } ) => { | ||
| const [ kind, name, record ] = args; | ||
| const recordId = await dispatch( getRecordPk( kind, name, record ) ); | ||
|
|
||
| const result = await dispatch( saveEntityRecord( ...args ) ); | ||
| if ( ! result ) { | ||
| if ( recordId ) { | ||
| const error = select.getLastEntitySaveError( kind, name, recordId ); | ||
| throw error; | ||
| } else { | ||
| throw new Error( 'Something went wrong' ); | ||
| } | ||
| } | ||
|
|
||
| const resultId = await dispatch( getRecordPk( kind, name, result ) ); | ||
| const error = select.getLastEntitySaveError( kind, name, resultId ); | ||
|
|
||
| if ( error ) { | ||
| throw error; | ||
| } | ||
| return result; | ||
| }; | ||
|
|
||
| export const throwingSaveEditedEntityRecord = ( ...args ) => async ( { | ||
| dispatch, | ||
| select, | ||
| } ) => { | ||
| const result = await dispatch( saveEditedEntityRecord( ...args ) ); | ||
|
|
||
| const [ kind, name, id ] = args; | ||
| const error = select.getLastEntitySaveError( kind, name, id ); | ||
|
|
||
| if ( error || ! result ) { | ||
| throw error; | ||
| } | ||
| return result; | ||
| }; | ||
|
|
||
| export const throwingDeleteEntityRecord = ( ...args ) => async ( { | ||
| dispatch, | ||
| select, | ||
| } ) => { | ||
| const result = await dispatch( deleteEntityRecord( ...args ) ); | ||
|
|
||
| const [ kind, name, id ] = args; | ||
| const error = select.getLastEntityDeleteError( kind, name, id ); | ||
|
|
||
| if ( error || ! result ) { | ||
| throw error; | ||
| } | ||
| return result; | ||
| }; | ||
|
|
||
| const getRecordPk = ( kind, name, record ) => async ( { dispatch } ) => { | ||
| const entities = await dispatch( getKindEntities( kind ) ); | ||
| const entity = find( entities, { kind, name } ); | ||
| if ( ! entity || entity?.__experimentalNoFetch ) { | ||
| return; | ||
| } | ||
| const entityIdKey = entity.key || DEFAULT_ENTITY_KEY; | ||
| return record[ entityIdKey ]; | ||
| }; | ||
Uh oh!
There was an error while loading. Please reload this page.