FirebaseExtended / reactfire

Hooks, Context Providers, and Components that make it easy to interact with Firebase.
https://firebaseopensource.com/projects/firebaseextended/reactfire/
MIT License
3.51k stars 399 forks source link

cannot handle error from useFirestoreCollectionData #540

Open perryd01 opened 2 years ago

perryd01 commented 2 years ago

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

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

aadito123 commented 1 year ago

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 commented 1 year ago

tagging @jhuleatt to hopefully increase visibility

jaskaye17 commented 1 year ago

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.

Yey007 commented 1 year ago

Same here. Anyone have something on this?

beamercola commented 1 year ago

same

jbaldassari commented 1 year ago

useFirestoreDoc also has this issue.

IAmJamesMudge commented 6 months ago

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.

rangel-rangelov commented 4 months ago

Bump, same here.

samdeen commented 3 months ago

Bump, when will this be fixed?