2
votes

When a firebase security rule (.read/.write/.validate) fails, it fails silently with a FIREBASE WARNING: logged in the console. I would like to capture the failure and present the user with a custom message (eg. "not authorised" etc.) How can this be achieved with firebase / angularfire?

P.S: I tried adding a error handler function to the angularfire promise object as follows, but it does not seem to trigger the error handler function, although the success function does trigger correctly

$firebase(dataRef).$bind($scope, "data").then(
            function () {
                console.log("success");
            },
            function (error) {
                console.log("error encountered: " + error);
            }
            );
1
It is currently not possible to intercept these errors in AngularFire, this will be fixed in a subsequent release. In the mean time, you can use the regular Firebase SDK - the .on() function takes a second callback that will be called when there's a security error: firebase.com/docs/javascript/firebase/on.html - Anant
@Anant Would .on() be used in addition to regular angularfire binding ($firebase(dataRef).$bind($scope, "data")) and the an additional statement to the the .on() ? Or did you mean, .on() in lieu of angularfire binding? Please clarify. An example, if possible, would be especially helpful. Thanks. - Jarnal
I mean using .on() in lieu of angularfire (for the purposes of catching the error). I'll post an example as an answer. - Anant

1 Answers

3
votes

You can use the regular Firebase API to catch security errors like this:

dataRef.on("value", function(snapshot) {
  $scope.data = snapshot.val();
}, function(err) {
  // Security error occurred.
});