RTK Query: How can you force dispatch an API error on null
HTTP 200 responses?
#2293
-
Hi all, Our UI is interfacing with a backend that returns HTTP 200s with We want to these instances to be considered an error in our UI code so our components can render the proper error views. I wrote a "null-checking" middleware to try to force RTK Query to dispatch an error: import { isFulfilled } from '@reduxjs/toolkit';
const nullCheckMiddleware = () => (next) => (action) => {
if (isFulfilled(action) && action?.type.startsWith('appApi') && action?.payload === null) {
return next({
...action,
type: 'appApi/executeQuery/rejected',
error: 'The response was null!'
})
}
} And I register this middleware with my store export const store = configureStore({
reducer: reducers,
middleware: (getDefaultMiddleware) =>
getDefaultMiddleware().concat([api.middleware, nullCheckMiddleware]),
}) I thought it was working well at first but it seems like it causes query hooks to go into an infinite loop of Any ideas if I'm on the right track? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Not really - do this in a |
Beta Was this translation helpful? Give feedback.
Not really - do this in a
baseQuery
wrapper, not in a middleware.