I am building an app with Angular 7, Firebase and AngularFire2. I want to detect a no network error occurring when trying to perform CRUD operations with Firestore.
At first I was thinking that I can use something like an HttpInterceptor
and do the following:
if (!window.navigator.onLine) {
return throwError(new NoNetworkConnectionError('No network connection'));
} else {
return next.handle(request);
}
Then, I thought, I can catch the error in a try-catch
block and determine whether it is an instance of my custom NoNetworkConnectionError
type. However, it didn't work, because I can't really intercept the calls that AngularFire2 is making with an HttpInterceptor, so I am pretty much stuck.
How can I solve this?
UPDATE:
I am not trying to check whether Firestore is online or not, but whether the user has Internet connection or not. If it was a simple HTTP request done with the HttpClient
in Angular, that could be solved by following the above steps. However, the HTTP requests that perform the CRUD operations in Firestore happen within the AngularFire2
library and I can't come up with a way to capture this. So, if I try to do something with Firestore, while my Internet is off, I cannot catch this error, I am just getting
POST https ://firestore.googleapis.com/...net::ERR_INTERNET_DISCONNECTED