I'm using RTKQuery to fetch data as following:
export const productsApi = createApi({
reducerPath: 'productsApi',
baseQuery: fetchBaseQuery({
baseUrl: BASE_URL,
prepareHeaders,
}),
tagTypes: ['Products'],
keepUnusedDataFor: 8600,
endpoints: builder => ({
getProperties: builder.query<IProduct[], IParams>({
query: params => ({ url: 'products', params: { per_page: 40, ...params } }),
transformResponse: ({ data }: { data: IProduct[] }) => data,
providesTags: ['Products'],
}),
});
I am relatively new to Redux Toolkit and started it using directly before using any of Redux. From the ReduxToolkit documentation, I'm finding 2 ways to catch and put actions on the backend.
- Using Async thunk.
- Middleware
I tried using the code as follows with the Middleware approach:
export const rtkQueryErrorLogger: Middleware = (api: MiddlewareAPI) => next => action => {
if (isRejected(action)) {
console.log(action);
}
return next(action);
}
But this does not catch the 40x/50x errors. What should be the way to handle this?
The purpose is to say send logs to the server upon catching these errors with meta-information. I was following: https://redux-toolkit.js.org/rtk-query/usage/error-handling#handling-errors-at-a-macro-level as a reference.