Skip to content

Commit 295a5d5

Browse files
authored
refactor: integration should throw error (#9130)
* feat(query-core): made "shouldThrowError" function * refactor(react-query): apply "shouldThrowError" function in react-query package * refactor(angular-query-experimental): apply "shouldThrowError" function * refactor(solid-query): apply "shouldThrowError" function * refactor(vue-query): apply "shouldThrowError" function * test: add shouldThrowError function test
1 parent e0ea8f2 commit 295a5d5

File tree

15 files changed

+54
-62
lines changed

15 files changed

+54
-62
lines changed

packages/angular-query-experimental/src/create-base-query.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ import {
77
signal,
88
untracked,
99
} from '@angular/core'
10-
import { QueryClient, notifyManager } from '@tanstack/query-core'
10+
import {
11+
QueryClient,
12+
notifyManager,
13+
shouldThrowError,
14+
} from '@tanstack/query-core'
1115
import { signalProxy } from './signal-proxy'
12-
import { shouldThrowError } from './util'
1316
import { injectIsRestoring } from './inject-is-restoring'
1417
import type {
1518
QueryKey,

packages/angular-query-experimental/src/inject-mutation.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ import {
1313
MutationObserver,
1414
QueryClient,
1515
notifyManager,
16+
shouldThrowError,
1617
} from '@tanstack/query-core'
1718
import { signalProxy } from './signal-proxy'
18-
import { noop, shouldThrowError } from './util'
19+
import { noop } from './util'
1920
import type { DefaultError, MutationObserverResult } from '@tanstack/query-core'
2021
import type { CreateMutateFunction, CreateMutationResult } from './types'
2122
import type { CreateMutationOptions } from './mutation-options'
Original file line numberDiff line numberDiff line change
@@ -1,13 +1 @@
1-
export function shouldThrowError<T extends (...args: Array<any>) => boolean>(
2-
throwError: boolean | T | undefined,
3-
params: Parameters<T>,
4-
): boolean {
5-
// Allow throwError function to override throwing behavior on a per-error basis
6-
if (typeof throwError === 'function') {
7-
return throwError(...params)
8-
}
9-
10-
return !!throwError
11-
}
12-
131
export function noop(): void {}

packages/query-core/src/__tests__/utils.test.tsx

+19
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
partialMatchKey,
1313
replaceEqualDeep,
1414
shallowEqualObjects,
15+
shouldThrowError,
1516
} from '../utils'
1617
import { Mutation } from '../mutation'
1718

@@ -530,4 +531,22 @@ describe('core/utils', () => {
530531
expect(hashKey(nested1)).toEqual(hashKey(nested2))
531532
})
532533
})
534+
535+
describe('shouldThrowError', () => {
536+
it('should return the result of executing throwOnError if throwOnError parameter is a function', () => {
537+
const throwOnError = (error: Error) => error.message === 'test error'
538+
expect(shouldThrowError(throwOnError, [new Error('test error')])).toBe(
539+
true,
540+
)
541+
expect(shouldThrowError(throwOnError, [new Error('other error')])).toBe(
542+
false,
543+
)
544+
})
545+
546+
it('should return throwOnError parameter itself if throwOnError is not a function', () => {
547+
expect(shouldThrowError(true, [new Error('test error')])).toBe(true)
548+
expect(shouldThrowError(false, [new Error('test error')])).toBe(false)
549+
expect(shouldThrowError(undefined, [new Error('test error')])).toBe(false)
550+
})
551+
})
533552
})

packages/query-core/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export {
2121
matchMutation,
2222
keepPreviousData,
2323
skipToken,
24+
shouldThrowError,
2425
} from './utils'
2526
export type { MutationFilters, QueryFilters, Updater, SkipToken } from './utils'
2627
export { isCancelledError } from './retryer'

packages/query-core/src/utils.ts

+12
Original file line numberDiff line numberDiff line change
@@ -430,3 +430,15 @@ export function ensureQueryFn<
430430

431431
return options.queryFn
432432
}
433+
434+
export function shouldThrowError<T extends (...args: Array<any>) => boolean>(
435+
throwOnError: boolean | T | undefined,
436+
params: Parameters<T>,
437+
): boolean {
438+
// Allow throwOnError function to override throwing behavior on a per-error basis
439+
if (typeof throwOnError === 'function') {
440+
return throwOnError(...params)
441+
}
442+
443+
return !!throwOnError
444+
}

packages/react-query/src/errorBoundaryUtils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client'
22
import * as React from 'react'
3-
import { shouldThrowError } from './utils'
3+
import { shouldThrowError } from '@tanstack/query-core'
44
import type {
55
DefaultedQueryObserverOptions,
66
Query,

packages/react-query/src/useMutation.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
'use client'
22
import * as React from 'react'
3-
import { MutationObserver, notifyManager } from '@tanstack/query-core'
3+
import {
4+
MutationObserver,
5+
notifyManager,
6+
shouldThrowError,
7+
} from '@tanstack/query-core'
48
import { useQueryClient } from './QueryClientProvider'
5-
import { noop, shouldThrowError } from './utils'
9+
import { noop } from './utils'
610
import type {
711
UseMutateFunction,
812
UseMutationOptions,

packages/react-query/src/utils.ts

-12
Original file line numberDiff line numberDiff line change
@@ -1,13 +1 @@
1-
export function shouldThrowError<T extends (...args: Array<any>) => boolean>(
2-
throwError: boolean | T | undefined,
3-
params: Parameters<T>,
4-
): boolean {
5-
// Allow throwError function to override throwing behavior on a per-error basis
6-
if (typeof throwError === 'function') {
7-
return throwError(...params)
8-
}
9-
10-
return !!throwError
11-
}
12-
131
export function noop(): void {}

packages/solid-query/src/useBaseQuery.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Had to disable the lint rule because isServer type is defined as false
22
// in solid-js/web package. I'll create a GitHub issue with them to see
33
// why that happens.
4-
import { hydrate, notifyManager } from '@tanstack/query-core'
4+
import { hydrate, notifyManager, shouldThrowError } from '@tanstack/query-core'
55
import { isServer } from 'solid-js/web'
66
import {
77
createComputed,
@@ -13,7 +13,6 @@ import {
1313
} from 'solid-js'
1414
import { createStore, reconcile, unwrap } from 'solid-js/store'
1515
import { useQueryClient } from './QueryClientProvider'
16-
import { shouldThrowError } from './utils'
1716
import { useIsRestoring } from './isRestoring'
1817
import type { UseBaseQueryOptions } from './types'
1918
import type { Accessor, Signal } from 'solid-js'
@@ -230,7 +229,7 @@ export function useBaseQuery<
230229
Fixes #7275
231230
In a few cases, the observer could unmount before the resource is loaded.
232231
This leads to Suspense boundaries to be suspended indefinitely.
233-
This resolver will be called when the observer is unmounting
232+
This resolver will be called when the observer is unmounting
234233
but the resource is still in a loading state
235234
*/
236235
let resolver: ((value: ResourceData) => void) | null = null

packages/solid-query/src/useMutation.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { MutationObserver } from '@tanstack/query-core'
1+
import { MutationObserver, shouldThrowError } from '@tanstack/query-core'
22
import { createComputed, createMemo, on, onCleanup } from 'solid-js'
33
import { createStore } from 'solid-js/store'
44
import { useQueryClient } from './QueryClientProvider'
5-
import { noop, shouldThrowError } from './utils'
5+
import { noop } from './utils'
66
import type { DefaultError } from '@tanstack/query-core'
77
import type { QueryClient } from './QueryClient'
88
import type {

packages/solid-query/src/utils.ts

-12
Original file line numberDiff line numberDiff line change
@@ -1,13 +1 @@
1-
export function shouldThrowError<T extends (...args: Array<any>) => boolean>(
2-
throwError: boolean | T | undefined,
3-
params: Parameters<T>,
4-
): boolean {
5-
// Allow throwError function to override throwing behavior on a per-error basis
6-
if (typeof throwError === 'function') {
7-
return throwError(...params)
8-
}
9-
10-
return !!throwError
11-
}
12-
131
export function noop(): void {}

packages/vue-query/src/useBaseQuery.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ import {
99
toRefs,
1010
watch,
1111
} from 'vue-demi'
12+
import { shouldThrowError } from '@tanstack/query-core'
1213
import { useQueryClient } from './useQueryClient'
13-
import { cloneDeepUnref, shouldThrowError, updateState } from './utils'
14+
import { cloneDeepUnref, updateState } from './utils'
1415
import type { Ref } from 'vue-demi'
1516
import type {
1617
DefaultedQueryObserverOptions,

packages/vue-query/src/useMutation.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import {
99
toRefs,
1010
watch,
1111
} from 'vue-demi'
12-
import { MutationObserver } from '@tanstack/query-core'
13-
import { cloneDeepUnref, shouldThrowError, updateState } from './utils'
12+
import { MutationObserver, shouldThrowError } from '@tanstack/query-core'
13+
import { cloneDeepUnref, updateState } from './utils'
1414
import { useQueryClient } from './useQueryClient'
1515
import type { ToRefs } from 'vue-demi'
1616
import type {

packages/vue-query/src/utils.ts

-12
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,3 @@ function isPlainObject(value: unknown): value is Object {
109109
function isFunction(value: unknown): value is Function {
110110
return typeof value === 'function'
111111
}
112-
113-
export function shouldThrowError<T extends (...args: Array<any>) => boolean>(
114-
throwOnError: boolean | T | undefined,
115-
params: Parameters<T>,
116-
): boolean {
117-
// Allow throwOnError function to override throwing behavior on a per-error basis
118-
if (typeof throwOnError === 'function') {
119-
return throwOnError(...params)
120-
}
121-
122-
return !!throwOnError
123-
}

0 commit comments

Comments
 (0)