diff --git a/packages/cozy-client/src/StackLink.js b/packages/cozy-client/src/StackLink.js index deaa22bafb..038511a66e 100644 --- a/packages/cozy-client/src/StackLink.js +++ b/packages/cozy-client/src/StackLink.js @@ -5,6 +5,7 @@ import CozyLink from './CozyLink' import { DOCTYPE_FILES } from './const' import { BulkEditError } from './errors' import logger from './logger' +import { isReactNativeOfflineError } from './utils' /** * @@ -84,10 +85,17 @@ export default class StackLink extends CozyLink { return forward(operation) } - if (operation.mutationType) { - return this.executeMutation(operation, result, forward) + try { + if (operation.mutationType) { + return await this.executeMutation(operation, result, forward) + } + return await this.executeQuery(operation) + } catch (err) { + if (isReactNativeOfflineError(err)) { + return forward(operation) + } + throw err } - return this.executeQuery(operation) } async persistData(data, forward) { diff --git a/packages/cozy-client/src/utils.js b/packages/cozy-client/src/utils.js index 973ac5cf37..8ac4fe55b3 100644 --- a/packages/cozy-client/src/utils.js +++ b/packages/cozy-client/src/utils.js @@ -68,3 +68,15 @@ export const hasQueriesBeenLoaded = queriesResults => { hasQueryBeenLoaded(queryResult) ) } + +/** + * Check is the error is about ReactNative not having access to internet + * + * @param {Error} err - The error to check + * @returns {boolean} True if the error is a network error, otherwise false + */ +export const isReactNativeOfflineError = err => { + // This error message is specific to ReactNative + // Network errors on a browser would produce another error.message + return err.message === 'Network request failed' +}