reactfire icon indicating copy to clipboard operation
reactfire copied to clipboard

cannot handle error from useFirestoreCollectionData

Open perryd01 opened this issue 3 years ago • 9 comments

Version info

React: 18.2.0

Firebase: 9.9.0

ReactFire: 4.2.2

Other (e.g. Node, browser, operating system) (if applicable): Next.js 12.2.2

Test case

use useFirestoreCollectionData and handle FirebaseError: Missing or insufficient permissions.

Steps to reproduce

  • Set permissions in firebase console and set a rule that doesn't allow the query for a user
  • Make the query
const { status, data: users, error } = useFirestoreCollectionData(usersQuery);

Expected behavior

The hook doesn't throw and the error is in error variable

Actual behavior

The hook throws and error cannot be handled without try catch (that should't be used?) https://github.com/FirebaseExtended/reactfire/blob/9754f8690389feaa1a7a65348aab1567857bca72/src/useObservable.ts#L120-L134

perryd01 avatar Aug 20 '22 20:08 perryd01

I have a similar issue with useFirestoreDocData and useFirestoreDocDataOnce.

The error actually originates from the SuspenseSubject class which throws an error when the value property is read while the Subject has an error i.e. Firebase Permission Error.

This error is also cannot be caught without a React Error Boundary. I think it would be better to pass in a flag like throwErrorInObservable that allows it to return the initialValue or just undefined to represent the error state. Would be more convenient to check for errors synchronously using the error attribute in the ObservableStatus. I would not mind making a PR for this if maintainers can sign off on it.

https://github.com/FirebaseExtended/reactfire/blob/9754f8690389feaa1a7a65348aab1567857bca72/src/SuspenseSubject.ts#L51-L61

Another approach could be to not set hasValue as true when there is an error however it seems that there might be a lot of code already dependent on such behaviour.

aadito123 avatar Sep 06 '22 10:09 aadito123

tagging @jhuleatt to hopefully increase visibility

aadito123 avatar Sep 13 '22 02:09 aadito123

Bumping this as well. Crashing on FirebaseError: Missing or insufficient permissions renders this library unusable for me as this is a valid use case which I am unable to handle.

jaskaye17 avatar Dec 03 '22 03:12 jaskaye17

Same here. Anyone have something on this?

utkudotdev avatar Dec 04 '22 03:12 utkudotdev

same

beamercola avatar Dec 20 '22 22:12 beamercola

useFirestoreDoc also has this issue.

jbaldassari avatar Jan 05 '23 05:01 jbaldassari

Bump. I can't use this library either because of this issue. Wrapping the entire function body in a try/catch does allow you to catch the error but is not tenable.

IAmJamesMudge avatar Feb 23 '24 20:02 IAmJamesMudge

Bump, same here.

rangel-rangelov avatar Apr 03 '24 14:04 rangel-rangelov

Bump, when will this be fixed?

samdeen avatar May 21 '24 10:05 samdeen