Android Question firestore - catching PERMISSION_DENIED exception

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
@DonManfred ,
I have am trying to access a collection which requires specific authentication.

Sometimes the authenticated user is not authorised to access the collection.

the Fetch call raises an exception, with PERMISSION_DENIED which is fine.

However, I cannot catch it in the B4A code. Is it possible to pass this exception up to B4A please.

Thanks

B4X:
Try
  collref.limit(100).fetch("allgames")
  wait for allgames_snapshot(gamessnap As QuerySnapshot)
    Catch
        Log("Cannot fetch " & LastException.Message)
        Return
      End Try
' ....other stuff

** Activity (firestoreoperations) Create, isFirst = true **
** Activity (firestoreoperations) Resume **
com.google.android.gms.tasks.RuntimeExecutionException: com.google.firebase.firestore.FirebaseFirestoreException: PERMISSION_DENIED: Missing or insufficient permissions.
at com.google.android.gms.tasks.zzu.getResult(Unknown Source:15)
at de.donmanfred.Querywrapper$2.onComplete(Querywrapper.java:138)
at com.google.android.gms.tasks.zzj.run(Unknown Source:4)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:180)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:835)
Caused by: com.google.firebase.firestore.FirebaseFirestoreException: PERMISSION_DENIED: Missing or insufficient permissions.
at com.google.firebase.firestore.util.Util.exceptionFromStatus(com.google.firebase:firebase-firestore@@19.0.0:119)
at com.google.firebase.firestore.core.EventManager.onError(com.google.firebase:firebase-firestore@@19.0.0:134)
at com.google.firebase.firestore.core.SyncEngine.handleRejectedListen(com.google.firebase:firebase-firestore@@19.0.0:399)
at com.google.firebase.firestore.core.FirestoreClient.handleRejectedListen(com.google.firebase:firebase-firestore@@19.0.0:265)
at com.google.firebase.firestore.remote.RemoteStore.processTargetError(com.google.firebase:firebase-firestore@@19.0.0:555)
at com.google.firebase.firestore.remote.RemoteStore.handleWatchChange(com.google.firebase:firebase-firestore@@19.0.0:436)
at com.google.firebase.firestore.remote.RemoteStore.access$100(com.google.firebase:firebase-firestore@@19.0.0:53)
at com.google.firebase.firestore.remote.RemoteStore$1.onWatchChange(com.google.firebase:firebase-firestore@@19.0.0:176)
at com.google.firebase.firestore.remote.WatchStream.onNext(com.google.firebase:firebase-firestore@@19.0.0:109)
at com.google.firebase.firestore.remote.WatchStream.onNext(com.google.firebase:firebase-firestore@@19.0.0:39)
at com.google.firebase.firestore.remote.AbstractStream$StreamObserver.lambda$onNext$1(com.google.firebase:firebase-firestore@@19.0.0:119)
at com.google.firebase.firestore.remote.AbstractStream$StreamObserver$$Lambda$2.run(Unknown Source:7)
at com.google.firebase.firestore.remote.AbstractStream$CloseGuardedRunner.run(com.google.firebase:firebase-firestore@@19.0.0:67)
at com.google.firebase.firestore.remote.AbstractStream$StreamObserver.onNext(com.google.firebase:firebase-firestore@@19.0.0:110)
at com.google.firebase.firestore.util.FirestoreChannel$1.onMessage(com.google.firebase:firebase-firestore@@19.0.0:150)
at io.grpc.ForwardingClientCallListener.onMessage(ForwardingClientCallListener.java:33)
at io.grpc.ForwardingClientCallListener.onMessage(ForwardingClientCallListener.java:33)
at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl$1MessagesAvailable.runInContext(ClientCallImpl.java:519)
at io.grpc.internal.ContextRunnable.run(ContextRunnable.java:37)
at io.grpc.internal.SerializingExecutor.run(SerializingExecutor.java:123)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:457)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at com.google.firebase.firestore.util.AsyncQueue$DelayedStartFactory.run(com.google.firebase:firebase-firestore@@19.0.0:205)
at java.lang.Thread.run(Thread.java:764)
Caused by: io.grpc.StatusException: PERMISSION_DENIED: Missing or insufficient permissions.
at io.grpc.Status.asException(Status.java:540)
at com.google.firebase.firestore.util.Util.exceptionFromStatus(com.google.firebase:firebase-firestore@@19.0.0:117)
... 26 more
 

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
Sorry for the delay in replying.

The new version 0.59 is still not right. Here is the code I am using


B4X:
    Private collref As CollectionReference = fs.collection($"gametype/${gametype}/games"$,"games")
    Private gamessnap As QuerySnapshot
    If (collref <> Null) Then
        Try
            Log("Before fetch")
            collref.limit(100).fetch("allgames")
            Log("Fetching")
            wait for allgames_snapshot(gameret As Object)
            Log("after snap")
            If (gameret Is QuerySnapshot) Then
                gamessnap = gameret
            Else
                Log("Query failed" & gamessnap)
                Return gamelist
            End If
            
        Catch
            Log("Cannot fetch " & LastException.Message)
            Return gamelist
        End Try

If I let the code just run I get:

Logger connected to: HTC HTC 10
--------- beginning of crash
--------- beginning of main
*** Service (starter) Create ***
** Service (starter) Start **
onAuthStateChanged: com.google.firebase.auth.internal.zzk@fd426b1
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Starter Signed in: com.google.firebase.auth.internal.zzm@bbf036f
auth.currentuser is initialisedtrue
Auth_SignedIn. precheck enquiries@digitwell.co.uk
*** Service (firestoreservice) Create ***
** Service (firestoreservice) Start **
*** Service (fbstorageservice) Create ***
** Service (fbstorageservice) Start **
auth.currentuser is initialisedtrue
Auth_SignedIn. precheck enquiries@digitwell.co.uk
** Activity (main) Pause, UserClosed = false **
** Activity (firestoreoperations) Create, isFirst = true **
** Activity (firestoreoperations) Resume **
Before fetch
Fetching
onFailure
after snap
java.lang.RuntimeException: java.lang.RuntimeException: java.io.EOFException
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:175)
at anywheresoftware.b4a.BA$2.run(BA.java:370)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:180)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:835)
Caused by: java.lang.RuntimeException: java.io.EOFException
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:175)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:176)
at anywheresoftware.b4a.shell.DebugResumableSub$RemoteResumableSub.resume(DebugResumableSub.java:22)
at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:250)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:137)
... 8 more
Caused by: java.io.EOFException
at java.io.DataInputStream.readByte(DataInputStream.java:270)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:344)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
... 12 more
*** Service (starter) Create ***
** Service (starter) Start **
onAuthStateChanged: com.google.firebase.auth.internal.zzk@fd426b1
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Starter Signed in: com.google.firebase.auth.internal.zzm@bbf036f
auth.currentuser is initialisedtrue
Auth_SignedIn. precheck enquiries@digitwell.co.uk
*** Service (firestoreservice) Create ***
** Service (firestoreservice) Start **
*** Service (fbstorageservice) Create ***
** Service (fbstorageservice) Start **
auth.currentuser is initialisedtrue
Auth_SignedIn. precheck enquiries@digitwell.co.uk
** Activity (main) Pause, UserClosed = false **
** Activity (firestoreoperations) Create, isFirst = true **
** Activity (firestoreoperations) Resume **
Before fetch
Fetching
com.google.android.gms.tasks.RuntimeExecutionException: com.google.firebase.firestore.FirebaseFirestoreException: PERMISSION_DENIED: Missing or insufficient permissions.

at com.google.android.gms.tasks.zzu.getResult(Unknown Source:15)
at de.donmanfred.Querywrapper$2.onComplete(Querywrapper.java:138)
at com.google.android.gms.tasks.zzj.run(Unknown Source:4)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:180)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:835)
Caused by: com.google.firebase.firestore.FirebaseFirestoreException: PERMISSION_DENIED: Missing or insufficient permissions.
at com.google.firebase.firestore.util.Util.exceptionFromStatus(com.google.firebase:firebase-firestore@@19.0.0:119)
at com.google.firebase.firestore.core.EventManager.onError(com.google.firebase:firebase-firestore@@19.0.0:134)
at com.google.firebase.firestore.core.SyncEngine.handleRejectedListen(com.google.firebase:firebase-firestore@@19.0.0:399)
at com.google.firebase.firestore.core.FirestoreClient.handleRejectedListen(com.google.firebase:firebase-firestore@@19.0.0:265)
at com.google.firebase.firestore.remote.RemoteStore.processTargetError(com.google.firebase:firebase-firestore@@19.0.0:555)
at com.google.firebase.firestore.remote.RemoteStore.handleWatchChange(com.google.firebase:firebase-firestore@@19.0.0:436)
at com.google.firebase.firestore.remote.RemoteStore.access$100(com.google.firebase:firebase-firestore@@19.0.0:53)
at com.google.firebase.firestore.remote.RemoteStore$1.onWatchChange(com.google.firebase:firebase-firestore@@19.0.0:176)
at com.google.firebase.firestore.remote.WatchStream.onNext(com.google.firebase:firebase-firestore@@19.0.0:109)
at com.google.firebase.firestore.remote.WatchStream.onNext(com.google.firebase:firebase-firestore@@19.0.0:39)
at com.google.firebase.firestore.remote.AbstractStream$StreamObserver.lambda$onNext$1(com.google.firebase:firebase-firestore@@19.0.0:119)
at com.google.firebase.firestore.remote.AbstractStream$StreamObserver$$Lambda$2.run(Unknown Source:7)
at com.google.firebase.firestore.remote.AbstractStream$CloseGuardedRunner.run(com.google.firebase:firebase-firestore@@19.0.0:67)
at com.google.firebase.firestore.remote.AbstractStream$StreamObserver.onNext(com.google.firebase:firebase-firestore@@19.0.0:110)
at com.google.firebase.firestore.util.FirestoreChannel$1.onMessage(com.google.firebase:firebase-firestore@@19.0.0:150)
at io.grpc.ForwardingClientCallListener.onMessage(ForwardingClientCallListener.java:33)
at io.grpc.ForwardingClientCallListener.onMessage(ForwardingClientCallListener.java:33)
at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl$1MessagesAvailable.runInContext(ClientCallImpl.java:519)
at io.grpc.internal.ContextRunnable.run(ContextRunnable.java:37)
at io.grpc.internal.SerializingExecutor.run(SerializingExecutor.java:123)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:457)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at com.google.firebase.firestore.util.AsyncQueue$DelayedStartFactory.run(com.google.firebase:firebase-firestore@@19.0.0:205)
at java.lang.Thread.run(Thread.java:764)
Caused by: io.grpc.StatusException: PERMISSION_DENIED: Missing or insufficient permissions.
at io.grpc.Status.asException(Status.java:540)
at com.google.firebase.firestore.util.Util.exceptionFromStatus(com.google.firebase:firebase-firestore@@19.0.0:117)
... 26 more

However, If I step through the code with the debugger then the code works.

*** Service (starter) Create ***
** Service (starter) Start **
onAuthStateChanged: com.google.firebase.auth.internal.zzk@fd426b1
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Starter Signed in: com.google.firebase.auth.internal.zzm@bbf036f
auth.currentuser is initialisedtrue
Auth_SignedIn. precheck enquiries@digitwell.co.uk
*** Service (firestoreservice) Create ***
** Service (firestoreservice) Start **
*** Service (fbstorageservice) Create ***
** Service (fbstorageservice) Start **
auth.currentuser is initialisedtrue
Auth_SignedIn. precheck enquiries@digitwell.co.uk
** Activity (main) Pause, UserClosed = false **
** Activity (firestoreoperations) Create, isFirst = true **
** Activity (firestoreoperations) Resume **
Before fetch
Fetching
onFailure
after snap
Query failed(QuerySnapshot) Not initialized
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…