You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Nov 23, 2024. It is now read-only.
* Add a basic exponential backoff, additional tests and helpers, example (#66)
* Add error handling copy
* Allow for a custom backoff fn
* Add video links and fix config
* Add additional tests for backoff fn
Co-authored-by: Matt Sutkowski <[email protected]>
Copy file name to clipboardExpand all lines: docs/concepts/error-handling.md
+175-2
Original file line number
Diff line number
Diff line change
@@ -5,6 +5,179 @@ sidebar_label: Error Handling
5
5
hide_title: true
6
6
---
7
7
8
-
# Error Handling
8
+
# `Error Handling`
9
9
10
-
## Coming Soon
10
+
If your query or mutation happens to throw an error when using [fetchBaseQuery](../api/fetchBaseQuery), it will be returned in the `error` property of the respective hook.
Ultimately, you can choose whatever library you prefer to use with your `baseQuery`, but it's important that you return the correct response format. If you haven't tried [`fetchBaseQuery`](../api/fetchBaseQuery) yet, give it a chance!
100
+
101
+
## Retrying on Error
102
+
103
+
RTK Query exports a utility called `retry` that you can wrap the `baseQuery` in your API definition with. It defaults to 5 attempts with a basic exponential backoff.
104
+
105
+
The default behavior would retry at these intervals:
106
+
107
+
1. 600ms + random time
108
+
2. 1200ms + random time
109
+
3. 2400ms + random time
110
+
4. 4800ms + random time
111
+
5. 9600ms + random time
112
+
113
+
```tstitle="Retry every request 5 times by default"
114
+
// maxRetries: 5 is the default, and can be omitted. Shown for documentation purposes.
In the event that you didn't want to retry on a specific endpoint, you can just set `maxRetries: 0`.
134
+
135
+
:::info
136
+
It is possible for a hook to return `data` and `error` at the same time. By default, RTK Query will keep whatever the last 'good' result was in `data` until it can be updated or garbage collected.
137
+
:::
138
+
139
+
## Bailing out of errors
140
+
141
+
`retry.fail`
142
+
143
+
```tstitle="TODO"
144
+
baseBaseQuery.mockImplementation((input) => {
145
+
retry.fail(error);
146
+
return { data: `this won't happen` };
147
+
});
148
+
149
+
constbaseQuery=retry(baseBaseQuery);
150
+
constapi=createApi({
151
+
baseQuery,
152
+
endpoints: (build) => ({
153
+
q1: build.query({
154
+
query: () => {},
155
+
}),
156
+
}),
157
+
});
158
+
```
159
+
160
+
## Handling errors at a macro level
161
+
162
+
There are quite a few ways that you can manage your errors, and in some cases, you may want to show a generic toast notification for any async error. Being that RTK Query is built on top of Redux and Redux-Toolkit, you can easily add a middleware to your store for this purpose.
163
+
164
+
:::tip
165
+
Redux-Toolkit released [matching utilities](https://redux-toolkit.js.org/api/matching-utilities#matching-utilities) in v1.5 that we can leverage for a lot of custom behaviors.
Copy file name to clipboardExpand all lines: docs/concepts/mutations.md
+1-1
Original file line number
Diff line number
Diff line change
@@ -234,7 +234,7 @@ When `addPost` is fired, it will only cause the `PostsList` to go into an `isFet
234
234
This is an example of a [CRUD service](https://en.wikipedia.org/wiki/Create,_read,_update_and_delete) for Posts. This implements the [Selectively invalidating lists](#selectively-invalidating-lists) strategy and will most likely serve as a good foundation for real applications.
0 commit comments