Skip to content

Commit 2ff6041

Browse files
authored
feat(useQuery): enable the throwOnError option of useQuery.refetch (TanStack#907)
* feat(useQuery): allow useQuery.refetch to throw an error when the fetch fails Enables the `throwOnError` configuration option for `useQuery.refetch` that has as of yet only been present in the documentation but not in the code. Relates to: TanStack#903 TanStack#843 * refactor(useQuery): move "useQuery.refetch" options to dedicated type
1 parent 7d25d2f commit 2ff6041

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

src/core/query.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ export interface FetchMoreOptions {
5858
previous: boolean
5959
}
6060

61+
export interface RefetchOptions {
62+
throwOnError?: boolean;
63+
}
64+
6165
export enum ActionType {
6266
Failed = 'Failed',
6367
MarkStale = 'MarkStale',
@@ -224,12 +228,16 @@ export class Query<TResult, TError> {
224228
)
225229
}
226230

227-
async refetch(): Promise<void> {
231+
async refetch(options?: RefetchOptions): Promise<TResult | undefined> {
228232
try {
229-
await this.fetch()
233+
return await this.fetch()
230234
} catch (error) {
235+
if (options?.throwOnError === true) {
236+
throw error;
237+
}
231238
Console.error(error)
232239
}
240+
return;
233241
}
234242

235243
heal(): void {

src/core/queryObserver.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { getStatusProps, isServer, isDocumentVisible, Console } from './utils'
22
import type { QueryResult, QueryObserverConfig } from './types'
3-
import type { Query, QueryState, Action, FetchMoreOptions } from './query'
3+
import type { Query, QueryState, Action, FetchMoreOptions, RefetchOptions } from './query'
44

55
export type UpdateListener<TResult, TError> = (
66
result: QueryResult<TResult, TError>
@@ -86,9 +86,9 @@ export class QueryObserver<TResult, TError> {
8686
return this.currentQuery.clear()
8787
}
8888

89-
async refetch(): Promise<void> {
89+
async refetch(options?: RefetchOptions): Promise<TResult | undefined> {
9090
this.currentQuery.updateConfig(this.config)
91-
return this.currentQuery.refetch()
91+
return this.currentQuery.refetch(options)
9292
}
9393

9494
async fetchMore(

src/core/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Query, FetchMoreOptions } from './query'
1+
import type { Query, FetchMoreOptions, RefetchOptions } from './query';
22
import type { QueryCache } from './queryCache'
33

44
export type QueryKey =
@@ -165,7 +165,7 @@ export interface QueryResultBase<TResult, TError = unknown> {
165165
isStale: boolean
166166
isSuccess: boolean
167167
query: Query<TResult, TError>
168-
refetch: () => Promise<void>
168+
refetch: (options?: RefetchOptions) => Promise<TResult | undefined>
169169
status: QueryStatus
170170
updatedAt: number
171171
}

0 commit comments

Comments
 (0)