Skip to content

Commit ca6071c

Browse files
committed
feat: Forward operation in StackLink.request() on Network error
On the Flagship app we want to serve `.query()` request using the `StackLink` when the device is connected, but we want to fallback to the `PouchLink` when we detect a connection loss In previous commit we tried pro-actively detect for connexion loss by calling an `isOnline()` method before processing the request But we want to also catch network errors when the `isOnline()` methods fails to detect connection loss, then we also fallback to the next `Link`
1 parent 3e1da95 commit ca6071c

2 files changed

Lines changed: 23 additions & 3 deletions

File tree

packages/cozy-client/src/StackLink.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import CozyLink from './CozyLink'
55
import { DOCTYPE_FILES } from './const'
66
import { BulkEditError } from './errors'
77
import logger from './logger'
8+
import { isReactNativeOfflineError } from './utils'
89

910
/**
1011
*
@@ -84,10 +85,17 @@ export default class StackLink extends CozyLink {
8485
return forward(operation)
8586
}
8687

87-
if (operation.mutationType) {
88-
return this.executeMutation(operation, result, forward)
88+
try {
89+
if (operation.mutationType) {
90+
return await this.executeMutation(operation, result, forward)
91+
}
92+
return await this.executeQuery(operation)
93+
} catch (err) {
94+
if (isReactNativeOfflineError(err)) {
95+
return forward(operation)
96+
}
97+
throw err
8998
}
90-
return this.executeQuery(operation)
9199
}
92100

93101
async persistData(data, forward) {

packages/cozy-client/src/utils.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,15 @@ export const hasQueriesBeenLoaded = queriesResults => {
6868
hasQueryBeenLoaded(queryResult)
6969
)
7070
}
71+
72+
/**
73+
* Check is the error is about ReactNative not having access to internet
74+
*
75+
* @param {Error} err - The error to check
76+
* @returns {boolean} True if the error is a network error, otherwise false
77+
*/
78+
export const isReactNativeOfflineError = err => {
79+
// This error message is specific to ReactNative
80+
// Network errors on a browser would produce another error.message
81+
return err.message === 'Network request failed'
82+
}

0 commit comments

Comments
 (0)