0

Firebase spits out an error if a request takes too long to resolve due to slow internet connections. Here is the error:

@firebase/firestore: Firestore (7.9.2): Could not reach Cloud Firestore backend. Connection failed 1 times. Most recent error: FirebaseError: [code=unavailable]: The operation could not be completed This typically indicates that your device does not have a healthy Internet connection at the moment. The client will operate in offline mode until it is able to successfully connect to the backend.

I ran into an odd behaviour with redux-thunks when trying to catch this error. My api function was able to catch the firebase error in its catch() block but was not caught in the catch() block within the thunk action. Here is some pseudocode to illustrate this better:

api.js

getDoc () {
  return firebase.firestore().doc().get().then(result => {
    return result
  }).catch(error => {
    //error caught here
    console.log(error)
  })
}

thunks.js

action () {
  api.getDoc().then(result => {
    //result contains the error
    dispatch(success(result));
  })
  .catch(error => {
    //not being reached
    dispatch(failed(error))
  })
}

I created a code sandbox to reproduce the error. Feel free to play around with the code. When you go offline while the app is fetching data, the firebase error will get thrown, and the console will show that the backend api caught the error but the thunk action failed to do so. I am not certain if this is an error on my part or a limitation of the redux-thunk library or firebase library.

Question: How can I handle this error so my thunk action does not dispatch an error as a success?

Any help will be warmly welcomed.

dillon
  • 71
  • 5

2 Answers2

1

That message is just a notice to you. It's not really an error, and you can't catch it. Firestore doesn't treat networks problems as errors. It just continually retries in the background.

Doug Stevenson
  • 236,239
  • 27
  • 275
  • 302
  • going from the sandbox I linked, when I go offline, the error is thrown, but if I go back online I get no indication that firebase is retrying the request in the background. The major problem I'm having is with the thunk action. Since the `then()` block of the thunk is triggered, which usually contains `dispatch(success)`, adding code in the then() block to check for an error seems counter-intuitive to me. Is there a way I can execute code when the firebase-retry succeeds? If I cannot catch that error, can you suggest a workflow I should use to handle such a case with redux-thunks? – dillon Mar 07 '20 at 01:47
  • No, you have no way of telling if Firestore is retrying anything. Queries typically only fail if they violate security rules or exceed some limit. – Doug Stevenson Mar 07 '20 at 02:00
  • Any way of reducing the retry-limit? Currently if executed in offline mode, some Firestore requests seem to take several minutes before throwing the connectivity/auth errors. – Janaka Bandara Apr 23 '21 at 10:09
0

Here is a solution in the event anyone comes across this error.

The simplest solution is to remove the catch() block from the api function and catch errors at the caller function. Based on the example code provided in the question, the only modification would be to the api.js file:

getDoc () {
  return firebase.firestore().doc().get().then(result => {
    return result
  })
}

While testing, if the error gets thrown by firebase, the thunk's catch() block is hit which dispatches the error action.

dillon
  • 71
  • 5