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. - Lưu ý v5:
onErrorở useMutation vẫn hoạt động nhưng globalonErrortrong defaultOptions đã bị xóa.
React Query v5 changed error handling compared to v4: the onError callback is deprecated, replaced by throwOnError or meta.
- Retry strategies: default 3 retries with exponential backoff — override:
retry: (failureCount, error) => error.status !== 401 && failureCount < 3to skip retrying auth errors. - Global error handling with 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: truecauses the query to throw up to the Error Boundary instead of returningisError. - Or use
useSuspenseQuery— automatically throws to the Error Boundary.error.messagevserror.response.data: with the fetch API, HTTP errors do not throw automatically — you must checkif (!res.ok) throw new Error(res.statusText)in queryFn. v5 pitfall:onErrorin useMutation still works but globalonErrorin defaultOptions has been removed.