Skip to content
This repository has been archived by the owner on Jul 12, 2024. It is now read-only.

Commit

Permalink
Add error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
psealock committed Jun 1, 2020
1 parent 6c19881 commit f24f603
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
3 changes: 2 additions & 1 deletion packages/data/src/options/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ export function setIsRequesting( optionName ) {
};
}

export function setRequestingError( error ) {
export function setRequestingError( error, options ) {
return {
type: TYPES.SET_REQUESTING_ERROR,
error,
options
};
}

Expand Down
18 changes: 12 additions & 6 deletions packages/data/src/options/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,25 @@
*/
import TYPES from './action-types';

const getIsRequestingObject = ( options, value ) => {
return Object.keys( options ).reduce( ( result, name ) => {
result[ name ] = value;
return result;
}, {} );
};

const optionsReducer = (
state = { isRequesting: {}, isUpdating: false },
{ type, options, error, isUpdating, optionName }
) => {
switch ( type ) {
case TYPES.RECEIVE_OPTIONS:
const resolvedOptions = {};
Object.keys( options ).forEach( name => {
resolvedOptions[ name ] = false;
} );
state = {
...state,
...options,
isRequesting: {
...state.isRequesting,
...resolvedOptions
...getIsRequestingObject( options, false ),
},
};
break;
Expand All @@ -41,7 +44,10 @@ const optionsReducer = (
state = {
...state,
requestingError: error,
isRequesting: false,
isRequesting: {
...state.isRequesting,
...getIsRequestingObject( options, false ),
},
};
break;
case TYPES.SET_UPDATING_ERROR:
Expand Down
11 changes: 8 additions & 3 deletions packages/data/src/options/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function* getOptionsWithRequest( names ) {
yield receiveOptions( results );
return results;
} catch ( error ) {
yield setRequestingError( error );
yield setRequestingError( error, names );
return error;
}
}
Expand All @@ -37,8 +37,13 @@ export function* getOption( name ) {

const url = WC_ADMIN_NAMESPACE + '/options?options=' + names;
fetches[ names ] = true;
const result = yield apiFetch( { path: url } );
yield receiveOptions( result );

try {
const result = yield apiFetch( { path: url } );
yield receiveOptions( result );
} catch ( error ) {
yield setRequestingError( error, names );
}

// Delete the fetch after to allow wp data to handle cache invalidation.
delete fetches[ names ];
Expand Down

0 comments on commit f24f603

Please sign in to comment.