React Query v5 thay đổi error handling so với v4: onError callback bị deprecated, thay bằng throwOnError hoặc meta.
- Retry strategies: mặc định retry 3 lần, exponential backoff — override:
retry: (failureCount, error) => error.status !== 401 && failureCount < 3để không retry auth errors. - Global error handling với QueryCache observer:
new QueryClient({ queryCache: new QueryCache({ onError: (error, query) => { if (query.meta?.showErrorToast) toast.error(error.message) } }) })— per-query opt-in:useQuery({ ..., meta: { showErrorToast: true } }). - Error Boundary integration:
throwOnError: truekhiến query throw error lên Error Boundary thay vì returnisError. - Hoặc dùng
useSuspenseQuery— tự động throw lên Error Boundary.error.messagevserror.response.data: với fetch API, HTTP errors không tự throw — phải checkif (!res.ok) throw new Error(res.statusText)trong queryFn.
Pitfall v5: onError ở useMutation vẫn hoạt động nhưng global onError trong defaultOptions đã bị xóa.