Error Handling
Error Handling
Overview
If 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 Examples
:::tip If you need to access the error or success payload immediately after a mutation, you can chain .unwrap()
.
:::
Errors with a custom baseQuery
baseQuery
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 level
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.
:::tip
Redux Toolkit has action matching utilities that we can leverage for additional custom behaviors.
:::
Last updated