Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

212
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!