Ionic + Firebase: No internet = crash


#1

Hi,

I’m adding Firebase to my Ionic3 app. The app should be able to run even in case no internet connection is available. However, as the app.component.ts constructor runs firebase.initializeApp(...), I get this “ERROR Error: Network Error at auth.js:16

I hoped a try catch block would allow handling this situation or the initializeApp to return a promise, but none of these 2 apply.

Has anyone a similar experience and/or solution to share? Thanks in advance,


#2

The best I’ve come up with so far is writing a custom ErrorHandler:

In app.module.ts, replace the commented line (default handler) with the extended handler:

//{provide: ErrorHandler, useClass: IonicErrorHandler},
{provide: ErrorHandler, useClass: AppExceptionHandler }

The extended handler itself:

// import { ErrorHandler } from '@angular/core';
import { IonicErrorHandler } from 'ionic-angular';

export class AppExceptionHandler
  // implements ErrorHandler {
  extends IonicErrorHandler  {

  handleError(error){
    switch(error.message){
      case "Network Error" : { this.handleNetworkError(); break; }
      default : super.handleError(error);
    }
  }

  handleNetworkError(){
    console.log('handling "network down" situation');
  }
}

Even though it prevents the app from crashing (better for offline mode), I have no clue how good/ugly this solution is for my specific case with Firebase. Feedback welcome please :slight_smile:


#3

Big Ups @derceto, I have the same issue and still can’t figure out how to catch it until I saw this.


#4

Thank you for sharing, I am facing the same situation!