Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

shouldThrowError.md

52 lines1.2 KB

1---
2id: shouldThrowError
3title: shouldThrowError
4---
5​
6```ts
7function shouldThrowError<T>(throwOnError: boolean | T | undefined, params: Parameters<T>): boolean;
8```
9​
10Defined in: [packages/query-core/src/utils.ts:693](https://github.com/TanStack/query/blob/main/packages/query-core/src/utils.ts#L693)
11​
12Resolves a `throwOnError` option to a boolean.
13If `throwOnError` is a function, it is called with `params` (e.g. the error and, depending on the caller,
14additional context such as the query or mutation) and its result is returned, allowing the throwing
15behavior to be decided per error. Otherwise, `throwOnError` itself is coerced to a boolean (`undefined`
16resolves to `false`).
17​
18## Type Parameters
19​
20### T
21​
22`T` *extends* (...`args`: `any`[]) => `boolean`
23​
24## Parameters
25​
26### throwOnError
27​
28`boolean` \| `T` \| `undefined`
29​
30The `throwOnError` option: a boolean, a function that decides per error, or
31`undefined`.
32​
33### params
34​
35`Parameters`\<`T`\>
36​
37The arguments passed to `throwOnError` if it is a function.
38​
39## Returns
40​
41`boolean`
42​
43Whether the error should be thrown.
44​
45## Example
46​
47```ts
48const throwOnError =
49 query.state.error && typeof options.throwOnError === 'function'
50 ? shouldThrowError(options.throwOnError, [query.state.error, query])
51 : options.throwOnError
52```