Tim Tharratt
Tim Tharratt

Reputation: 1310

Angular firebase and observable error handling

I’m struggling to understand how to add some error detection to angular firebase when using the code below. When new users login to my website they don’t have any permissions on the database and an error is thrown in the console.

I’m just trying to get it so that if this error is thrown auth.isApproved returns false, at the moment it doesn’t return. I’ve tried adding a .catch to the .switchMap and that didn’t work.

Error in console:

Uncaught Error: permission_denied at /authorization/users/VAST9WnazFUc6u9PQ5x74bOPIsJ2: Client doesn't have permission to access the desired data.

Consuming component:

this.auth.isApproved().subscribe(approved => {
  console.log (approved);

AuthService (auth):

isApproved(): Observable<boolean> {
    return this.user$
    .switchMap(user => this.DataService.getSharesDBUser(user.uid).valueChanges())
    .map(appUser => appUser.isApproved)
  }

DataService (DataService)

getSharesDBUser(userUID : string ): AngularFireObject<firebaseDBUser> {
    return this.db.object('/authorization/users/' + userUID);
  }

Interface

export interface firebaseDBUser {
  isAdmin                  : boolean;
  isApproved               : boolean;
  name                     : string;
}

Upvotes: 0

Views: 2661

Answers (1)

No&#233;mi Sala&#252;n
No&#233;mi Sala&#252;n

Reputation: 5036

My first guess would be to add a catch operator on the switchMap inner observable, but you say that it doesn't work :/

isApproved(): Observable<boolean> {
    return this.user$.switchMap(user =>
        this.DataService.getSharesDBUser(user.uid).valueChanges()
            .map(appUser => appUser.isApproved)
            .catch(() => Observable.of(false))
    )
}

If the catch operator cannot catch this error, it should be because the error isn't triggered inside the valueChanges observable chain, maybe it's right before, in the this.db.object(...) function call. In this case, a simple try-catch around the call should do the trick.

Upvotes: 1

Related Questions