Skip to content

refactor: integration should throw error #9130

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions packages/angular-query-experimental/src/create-base-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ import {
signal,
untracked,
} from '@angular/core'
import { QueryClient, notifyManager } from '@tanstack/query-core'
import {
QueryClient,
notifyManager,
shouldThrowError,
} from '@tanstack/query-core'
import { signalProxy } from './signal-proxy'
import { shouldThrowError } from './util'
import { injectIsRestoring } from './inject-is-restoring'
import type {
QueryKey,
Expand Down
3 changes: 2 additions & 1 deletion packages/angular-query-experimental/src/inject-mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import {
MutationObserver,
QueryClient,
notifyManager,
shouldThrowError,
} from '@tanstack/query-core'
import { signalProxy } from './signal-proxy'
import { noop, shouldThrowError } from './util'
import { noop } from './util'
import type { DefaultError, MutationObserverResult } from '@tanstack/query-core'
import type { CreateMutateFunction, CreateMutationResult } from './types'
import type { CreateMutationOptions } from './mutation-options'
Expand Down
12 changes: 0 additions & 12 deletions packages/angular-query-experimental/src/util/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1 @@
export function shouldThrowError<T extends (...args: Array<any>) => boolean>(
throwError: boolean | T | undefined,
params: Parameters<T>,
): boolean {
// Allow throwError function to override throwing behavior on a per-error basis
if (typeof throwError === 'function') {
return throwError(...params)
}

return !!throwError
}

export function noop(): void {}
19 changes: 19 additions & 0 deletions packages/query-core/src/__tests__/utils.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
partialMatchKey,
replaceEqualDeep,
shallowEqualObjects,
shouldThrowError,
} from '../utils'
import { Mutation } from '../mutation'

Expand Down Expand Up @@ -530,4 +531,22 @@ describe('core/utils', () => {
expect(hashKey(nested1)).toEqual(hashKey(nested2))
})
})

describe('shouldThrowError', () => {
it('should return the result of executing throwOnError if throwOnError parameter is a function', () => {
const throwOnError = (error: Error) => error.message === 'test error'
expect(shouldThrowError(throwOnError, [new Error('test error')])).toBe(
true,
)
expect(shouldThrowError(throwOnError, [new Error('other error')])).toBe(
false,
)
})

it('should return throwOnError parameter itself if throwOnError is not a function', () => {
expect(shouldThrowError(true, [new Error('test error')])).toBe(true)
expect(shouldThrowError(false, [new Error('test error')])).toBe(false)
expect(shouldThrowError(undefined, [new Error('test error')])).toBe(false)
})
})
})
1 change: 1 addition & 0 deletions packages/query-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export {
matchMutation,
keepPreviousData,
skipToken,
shouldThrowError,
} from './utils'
export type { MutationFilters, QueryFilters, Updater, SkipToken } from './utils'
export { isCancelledError } from './retryer'
Expand Down
12 changes: 12 additions & 0 deletions packages/query-core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,3 +430,15 @@ export function ensureQueryFn<

return options.queryFn
}

export function shouldThrowError<T extends (...args: Array<any>) => boolean>(
throwOnError: boolean | T | undefined,
params: Parameters<T>,
): boolean {
// Allow throwOnError function to override throwing behavior on a per-error basis
if (typeof throwOnError === 'function') {
return throwOnError(...params)
}

return !!throwOnError
}
2 changes: 1 addition & 1 deletion packages/react-query/src/errorBoundaryUtils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client'
import * as React from 'react'
import { shouldThrowError } from './utils'
import { shouldThrowError } from '@tanstack/query-core'
import type {
DefaultedQueryObserverOptions,
Query,
Expand Down
8 changes: 6 additions & 2 deletions packages/react-query/src/useMutation.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
'use client'
import * as React from 'react'
import { MutationObserver, notifyManager } from '@tanstack/query-core'
import {
MutationObserver,
notifyManager,
shouldThrowError,
} from '@tanstack/query-core'
import { useQueryClient } from './QueryClientProvider'
import { noop, shouldThrowError } from './utils'
import { noop } from './utils'
import type {
UseMutateFunction,
UseMutationOptions,
Expand Down
12 changes: 0 additions & 12 deletions packages/react-query/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1 @@
export function shouldThrowError<T extends (...args: Array<any>) => boolean>(
throwError: boolean | T | undefined,
params: Parameters<T>,
): boolean {
// Allow throwError function to override throwing behavior on a per-error basis
if (typeof throwError === 'function') {
return throwError(...params)
}

return !!throwError
}

export function noop(): void {}
5 changes: 2 additions & 3 deletions packages/solid-query/src/useBaseQuery.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Had to disable the lint rule because isServer type is defined as false
// in solid-js/web package. I'll create a GitHub issue with them to see
// why that happens.
import { hydrate, notifyManager } from '@tanstack/query-core'
import { hydrate, notifyManager, shouldThrowError } from '@tanstack/query-core'
import { isServer } from 'solid-js/web'
import {
createComputed,
Expand All @@ -13,7 +13,6 @@ import {
} from 'solid-js'
import { createStore, reconcile, unwrap } from 'solid-js/store'
import { useQueryClient } from './QueryClientProvider'
import { shouldThrowError } from './utils'
import { useIsRestoring } from './isRestoring'
import type { UseBaseQueryOptions } from './types'
import type { Accessor, Signal } from 'solid-js'
Expand Down Expand Up @@ -230,7 +229,7 @@ export function useBaseQuery<
Fixes #7275
In a few cases, the observer could unmount before the resource is loaded.
This leads to Suspense boundaries to be suspended indefinitely.
This resolver will be called when the observer is unmounting
This resolver will be called when the observer is unmounting
but the resource is still in a loading state
*/
let resolver: ((value: ResourceData) => void) | null = null
Expand Down
4 changes: 2 additions & 2 deletions packages/solid-query/src/useMutation.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { MutationObserver } from '@tanstack/query-core'
import { MutationObserver, shouldThrowError } from '@tanstack/query-core'
import { createComputed, createMemo, on, onCleanup } from 'solid-js'
import { createStore } from 'solid-js/store'
import { useQueryClient } from './QueryClientProvider'
import { noop, shouldThrowError } from './utils'
import { noop } from './utils'
import type { DefaultError } from '@tanstack/query-core'
import type { QueryClient } from './QueryClient'
import type {
Expand Down
12 changes: 0 additions & 12 deletions packages/solid-query/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1 @@
export function shouldThrowError<T extends (...args: Array<any>) => boolean>(
throwError: boolean | T | undefined,
params: Parameters<T>,
): boolean {
// Allow throwError function to override throwing behavior on a per-error basis
if (typeof throwError === 'function') {
return throwError(...params)
}

return !!throwError
}

export function noop(): void {}
3 changes: 2 additions & 1 deletion packages/vue-query/src/useBaseQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import {
toRefs,
watch,
} from 'vue-demi'
import { shouldThrowError } from '@tanstack/query-core'
import { useQueryClient } from './useQueryClient'
import { cloneDeepUnref, shouldThrowError, updateState } from './utils'
import { cloneDeepUnref, updateState } from './utils'
import type { Ref } from 'vue-demi'
import type {
DefaultedQueryObserverOptions,
Expand Down
4 changes: 2 additions & 2 deletions packages/vue-query/src/useMutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import {
toRefs,
watch,
} from 'vue-demi'
import { MutationObserver } from '@tanstack/query-core'
import { cloneDeepUnref, shouldThrowError, updateState } from './utils'
import { MutationObserver, shouldThrowError } from '@tanstack/query-core'
import { cloneDeepUnref, updateState } from './utils'
import { useQueryClient } from './useQueryClient'
import type { ToRefs } from 'vue-demi'
import type {
Expand Down
12 changes: 0 additions & 12 deletions packages/vue-query/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,3 @@ function isPlainObject(value: unknown): value is Object {
function isFunction(value: unknown): value is Function {
return typeof value === 'function'
}

export function shouldThrowError<T extends (...args: Array<any>) => boolean>(
throwOnError: boolean | T | undefined,
params: Parameters<T>,
): boolean {
// Allow throwOnError function to override throwing behavior on a per-error basis
if (typeof throwOnError === 'function') {
return throwOnError(...params)
}

return !!throwOnError
}
Loading