-
Notifications
You must be signed in to change notification settings - Fork 403
fix: surface observable errors via status instead of re-throwing #735
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
b113bc3
776c993
74aee91
26cd552
c7d3d6a
13bfb96
91ea1ac
c6ddca8
e021670
0971021
7286c57
11286bf
0fde812
bacf14b
a26a9c0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| CLAUDE.local* | ||
| .DS_Store | ||
| npm-debug.log | ||
|
|
||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| { | ||
| "version": "4.2.3", | ||
| "version": "4.3.0", | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See "Semver" in my review — the description says breaking, this says minor. Worth an explicit 4.3.0-vs-5.0.0 decision (this number only drives |
||
| "license": "MIT", | ||
| "type": "module", | ||
| "main": "dist/index.umd.cjs", | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import * as React from 'react'; | ||
| import { getDownloadURL, fromTask } from 'rxfire/storage'; | ||
| import { defer } from 'rxjs'; | ||
| import { ReactFireOptions, useObservable, ObservableStatus, useStorage } from './'; | ||
| import { useSuspenseEnabledFromConfigAndContext } from './firebaseApp'; | ||
| import { ref } from 'firebase/storage'; | ||
|
|
@@ -28,7 +29,7 @@ export function useStorageTask<T = unknown>(task: UploadTask, ref: StorageRefere | |
| */ | ||
| export function useStorageDownloadURL<T = string>(ref: StorageReference, options?: ReactFireOptions<T>): ObservableStatus<string | T> { | ||
| const observableId = `storage:downloadUrl:${ref.toString()}`; | ||
| const observable$ = getDownloadURL(ref); | ||
| const observable$ = defer(() => getDownloadURL(ref)); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this fix related to the error behavior change? If not, could you please break it out into its own PR so we can include it in a v4 patch release? |
||
|
|
||
| return useObservable(observableId, observable$, options); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -71,7 +71,6 @@ export interface ObservableStatusSuccess<T> extends ObservableStatusBase<T> { | |
|
|
||
| export interface ObservableStatusError<T> extends ObservableStatusBase<T> { | ||
| status: 'error'; | ||
| isComplete: true; | ||
| error: Error; | ||
| } | ||
|
|
||
|
|
@@ -83,6 +82,17 @@ export interface ObservableStatusLoading<T> extends ObservableStatusBase<T> { | |
|
|
||
| export type ObservableStatus<T> = ObservableStatusLoading<T> | ObservableStatusError<T> | ObservableStatusSuccess<T>; | ||
|
|
||
| /** | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for adding this! |
||
| * Subscribe to an Observable and return its current status. | ||
| * | ||
| * Error handling depends on the suspense mode: | ||
| * - Non-suspense mode (default): errors are returned as `{ status: 'error', error }` so the | ||
| * component can handle them locally without needing a React Error Boundary. | ||
| * - Suspense mode (`suspense: true`): errors are re-thrown so a React Error Boundary can catch them. | ||
| * | ||
| * If the observable emits a value and then errors, `data` retains the last emitted value and | ||
| * `status` changes to `'error'`. There is no automatic retry path once an error occurs. | ||
| */ | ||
| export function useObservable<T = unknown>(observableId: string, source: Observable<T>, config: ReactFireOptions = {}): ObservableStatus<T> { | ||
| if (!observableId) { | ||
| throw new Error('cannot call useObservable without an observableId'); | ||
|
|
@@ -105,9 +115,8 @@ export function useObservable<T = unknown>(observableId: string, source: Observa | |
| next: () => { | ||
| onStoreChange(); | ||
| }, | ||
| error: (e) => { | ||
| error: () => { | ||
| onStoreChange(); | ||
| throw e; | ||
| }, | ||
| complete: () => { | ||
| onStoreChange(); | ||
|
|
@@ -145,9 +154,9 @@ export function useObservable<T = unknown>(observableId: string, source: Observa | |
| } as ObservableStatus<T>; | ||
| } | ||
|
|
||
| // throw an error if there is an error | ||
| // TODO(jhuleatt) this is the current, tested-for, behavior. But do we actually want it? | ||
| if (update.error) { | ||
| // In suspense mode, throw errors so React Error Boundaries can catch them. | ||
| // In non-suspense mode, surface errors via status so consumers can handle them locally. | ||
| if (suspenseEnabled && update.error) { | ||
|
tyler-reitz marked this conversation as resolved.
|
||
| throw update.error; | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,9 @@ import '@testing-library/jest-dom/extend-expect'; | |
| import { act, cleanup, render, renderHook, waitFor } from '@testing-library/react'; | ||
| import * as React from 'react'; | ||
| import { of, Subject, BehaviorSubject, throwError } from 'rxjs'; | ||
| import { useObservable } from '../src/index'; | ||
| import { useObservable, FirebaseAppProvider } from '../src/index'; | ||
| import { initializeApp } from 'firebase/app'; | ||
| import { baseConfig } from './appConfig'; | ||
|
|
||
| describe('useObservable', () => { | ||
| afterEach(cleanup); | ||
|
|
@@ -124,10 +126,46 @@ describe('useObservable', () => { | |
|
|
||
| act(() => observable$.next('val')); | ||
| expect(result.current.isComplete).toEqual(false); | ||
|
|
||
| act(() => observable$.complete()); | ||
| await waitFor(() => expect(result.current.isComplete).toEqual(true)); | ||
| }); | ||
|
|
||
| it('surfaces errors via status in non-suspense mode', async () => { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The #535 marquee scenario (
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added. Had to pair it with a |
||
| const error = new Error('I am an error'); | ||
| const observable$ = throwError(error); | ||
|
|
||
| const { result } = renderHook(() => useObservable('test-error-non-suspense', observable$, { suspense: false })); | ||
|
|
||
| await waitFor(() => expect(result.current.status).toEqual('error')); | ||
| expect(result.current.error).toEqual(error); | ||
| }); | ||
|
|
||
| it('surfaces errors via status when no suspense option is provided', async () => { | ||
| const error = new Error('default mode error'); | ||
| const observable$ = throwError(error); | ||
|
|
||
| const { result } = renderHook(() => useObservable('test-error-default-mode', observable$)); | ||
|
|
||
| await waitFor(() => expect(result.current.status).toEqual('error')); | ||
| expect(result.current.error).toEqual(error); | ||
| }); | ||
|
|
||
| it('retains last emitted data when observable errors after emitting', async () => { | ||
| const subject$ = new Subject<string>(); | ||
| const error = new Error('late error'); | ||
|
|
||
| const { result } = renderHook(() => useObservable('test-late-error', subject$, { suspense: false })); | ||
|
|
||
| act(() => subject$.next('good value')); | ||
| await waitFor(() => expect(result.current.status).toEqual('success')); | ||
| expect(result.current.data).toEqual('good value'); | ||
|
|
||
| act(() => subject$.error(error)); | ||
| await waitFor(() => expect(result.current.status).toEqual('error')); | ||
| expect(result.current.error).toEqual(error); | ||
| expect(result.current.data).toEqual('good value'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Suspense Mode', () => { | ||
|
|
@@ -328,5 +366,29 @@ describe('useObservable', () => { | |
| // if useObservable doesn't re-emit, the value here will still be "Jeff" | ||
| expect(refreshedComp).toHaveTextContent('James'); | ||
| }); | ||
| it('throws an error via FirebaseAppProvider suspense context path', () => { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Verified this exercises the context branch (hook called with no config, so the provider's |
||
| const spy = vi.spyOn(console, 'error'); | ||
| spy.mockImplementation(() => {}); | ||
|
|
||
| const onError = (e: ErrorEvent) => e.preventDefault(); | ||
| window.addEventListener('error', onError); | ||
|
|
||
| const app = initializeApp(baseConfig, 'suspense-context-test'); | ||
| const error = new Error('context-path error'); | ||
| const observable$ = throwError(error); | ||
|
|
||
| const wrapper = ({ children }: { children: React.ReactNode }) => ( | ||
| <FirebaseAppProvider firebaseApp={app} suspense={true}> | ||
| {children} | ||
| </FirebaseAppProvider> | ||
| ); | ||
|
|
||
| expect(() => renderHook(() => useObservable('test-context-suspense-error', observable$), { wrapper })).toThrow( | ||
| expect.objectContaining({ message: 'context-path error' }) | ||
| ); | ||
|
|
||
| spy.mockRestore(); | ||
| window.removeEventListener('error', onError); | ||
| }); | ||
| }); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of adding this here, can you please add a section to use.md with title
Error Handlingand document the two different ways? (suspense mode vs non-suspense mode)