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

error-handling.md

57 lines2.0 KB

1---
2id: error-handling
3title: Error Handling
4---
5​
6Query and mutation errors are exposed through their result signals. The adapter does not report
7failed requests to a global error handler automatically.
8​
9To report cache failures to Angular's `ErrorHandler`, configure the cache callbacks when creating
10the client. Capture the handler in the provider factory, where `inject()` is available:
11​
12```ts
13import { ErrorHandler, inject } from '@angular/core'
14import {
15 MutationCache,
16 QueryCache,
17 QueryClient,
18 provideTanStackQuery,
19} from '@tanstack/angular-query'
20​
21export const queryProviders = provideTanStackQuery(() => {
22 const errors = inject(ErrorHandler)
23​
24 return new QueryClient({
25 queryCache: new QueryCache({
26 onError: (error, query) => {
27 if (query.meta?.reportToErrorHandler !== false) {
28 errors.handleError(error)
29 }
30 },
31 }),
32 mutationCache: new MutationCache({
33 onError: (error, _variables, _context, mutation) => {
34 if (mutation.meta?.reportToErrorHandler !== false) {
35 errors.handleError(error)
36 }
37 },
38 }),
39 })
40})
41```
42​
43This example reports failures by default. Set `meta: { reportToErrorHandler: false }` on a query or
44mutation which your application handles locally. Compose these callbacks with any cache callbacks
45you already use.
46​
47A query cache callback reports a failed cache operation once, even when several components observe
48the query. Observer-only `select` errors and exceptions in options or `combine` computations are
49not cache failures and are not reported by this recipe.
50​
51`mutateAsync()` rejects and can be caught with `try`/`catch`. `mutate()` returns void; use mutation
52callbacks or result signals for its error state. `refetch({ throwOnError: true })` opts into promise
53rejection for that call.
54​
55The [Resource view](../resource-api.md) throws from `value()` when its mapped status is `error`.
56Check `hasValue()` before reading it. An uncaught error while Angular renders a template can still
57reach Angular's error handler. This integration does not provide a component error boundary.