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

Adding retry feature to mobile apiFetch handler #1686

Merged
merged 8 commits into from
Jan 31, 2020
11 changes: 9 additions & 2 deletions src/api-fetch-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import { fetchRequest } from 'react-native-gutenberg-bridge';
*/
import apiFetch from '@wordpress/api-fetch';

const fetchHandler = ( { path } ) => {
const setTimeoutPromise = ( delay ) => new Promise( ( resolve ) => setTimeout( resolve, delay ) );

const fetchHandler = ( { path }, retries = 20 ) => {
if ( ! isPathSupported( path ) ) {
return Promise.reject( `Unsupported path: ${ path }` );
}
Expand All @@ -27,7 +29,12 @@ const fetchHandler = ( { path } ) => {
return responsePromise.then( parseResponse ).catch( ( error ) => {
// eslint-disable-next-line no-console
console.warn( 'Network Error: ', error );
return Promise.resolve( error );
if ( error.code >= 400 && error.code < 600 ) {
return Promise.reject( error );
} else if ( retries === 0 ) {
return Promise.reject( error );
}
return setTimeoutPromise( 2000 ).then( () => fetchHandler( { path }, retries - 1 ) );
} );
};

Expand Down