#
Error Handling#
OverviewIf your query or mutation happens to throw an error when using fetchBaseQuery, it will be returned in the error
property of the respective hook. The component will re-render when that occurs, and you can show appropriate UI based on the error data if desired.
#
Error Display Examplestip
If you need to access the error or success payload immediately after a mutation, you can chain .unwrap()
.
baseQuery
#
Errors with a custom Whether a response is returned as data
or error
is dictated by the baseQuery
provided.
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
yet, give it a chance! Otherwise, see Customizing Queries for information on how to alter the returned errors.
#
Handling errors at a macro levelThere 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.
tip
Redux Toolkit has action matching utilities that we can leverage for additional custom behaviors.
- TypeScript
- JavaScript