Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

219
Visualizações
How to make RTK Query createApi's mutation throw rejection?

How to make RTK Query createApi's mutation throw rejection?

The current code execution will enter then, How to get here.

getList({ id }).catch(() => { How to get here });

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);
    }
}

services.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,
});
about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

This is so that your application is not littered with uncaught async exceptions if you are not interested in the result.

You can unwrap the result to get to "throwing" behaviour:

getList({ id }).unwrap().catch(() => { How to get here });

That said, it is very likely that getList should be a query, not a mutation. Mutations are things that change data on your server.

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda