3

I have a collection in Firebase that may occasionally be accessed by a user who doesn't have the right permissions. Right now when this happens, I'm getting a bunch of warnings and uncaught exceptions in the console and it seems to be impossible to handle them properly. I usually subscribe to an observable like this:

this.af.database.object('/someLocation/abc').subscribe(obj => {
    // logic
});

Without loosening security rules, what is the best way to handle these unhandled exceptions?

1 Answer 1

5

You can pass an error handler to your subscribe call:

this.af.database.object('/someLocation/abc').subscribe(
  obj => {
    // logic
  },
  error => {
    // handle/report the error
  }
);

This should remove an unhandled errors/rejected promises from the console, but it's likely that Firebase will still report warnings there.

Also, you should be aware that when observables error, any subscribers are automatically unsubscribed. So if you get an error, that's it; no further values will be emitted.

An alternative to specifying the error handler in subscribe, if you are composing an observable chain, is to use the catch operator:

import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of';
import 'rxjs/add/operator/catch';

let abc$ = this.af.database
  .object('/someLocation/abc')
  .catch(error => {
    // handle the error
    // and return an appropriate observable if you have one
    // perhaps a default value or an empty observable, etc.
    // or rethrow the error
    // or return Observable.throw(new Error('Some error'))
    return Observable.of("some-default-value");
  });
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I have no idea how I missed the error lambda. Probably been starting at this too long.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.