¿Cómo hacer que la mutación de RTK Query createApi arroje rechazo?
El código de ejecución actual entrará entonces, Cómo llegar aquí.
getList({ id }).catch(() => { Cómo llegar aquí });
baseQuery.js
import { fetchBaseQuery, retry } from '@reduxjs/toolkit/query/react'; const baseQueryWithRetry = retry( fetchBaseQuery({ baseUrl: process.env.REACT_APP_BASE_URL, }), { maxRetries: 6 } ); export const baseQueryWithReauth = async (args, api, extraOptions) => { try { const result = await baseQueryWithRetry(args, api, extraOptions) as { data: { RES: string } }; if (result.data.RES) { const { body } = JSON.parse(result.data.RES); switch (body.code) { case '000000': return { ...result, data: body } default: debugger throw new Error('body.message'); } } return result; } catch (err: any) { debugger return Promise.reject(err); } }servicios.js
import { createApi } from '@reduxjs/toolkit/query/react'; import { baseQueryWithReauth } from './baseQuery'; export const quoteApi = createApi({ reducerPath: 'quoteApi', baseQuery: baseQueryWithReauth, endpoints: (builder) => ({ getList: builder.mutation<any, object>({ query: (body) => ({ url: 'getPriceList', method: 'POST', body, }), }), }), refetchOnReconnect: true, });Esto es para que su aplicación no esté llena de excepciones asíncronas no detectadas si no está interesado en el resultado.
Puede unwrap el resultado para llegar al comportamiento de "lanzamiento":
getList({ id }).unwrap().catch(() => { How to get here }); Dicho esto, es muy probable que getList sea una consulta, no una mutación. Las mutaciones son cosas que cambian los datos en su servidor.