0

Our backend returns something like this for success:

{
      message: "Entry Succesfully Entered",
      errorCode: 0,
      data: {
        success: true,
      },
      info: [],
    });
  }

And something like this for failure:

{
    info: [],
    message: "Some info about the error",
    errorCode: 
}

The actual error code in the failure response is basically meaningless.

I've got my mutation transforming the response thusly:

transformResponse: ({ data, errorCode, message }) => {
  if (data?.success) return { success: true };

  return {
    error: {
      status: errorCode,
      message,
    },
  };
},

However this is always delivered in the data member of the result returned by the RTKQ hook.

Is there a way to affect the error (and possibly the isError) part of that returned result?

Jonathan Tuzman
  • 11,568
  • 18
  • 69
  • 129
  • The only thing you could do there is wrapping the `baseQuery` to handle that and return an `{ error: .... }` object in that case. – phry Mar 09 '22 at 07:24
  • @phry can you elaborate in detail? – Jonathan Tuzman Mar 09 '22 at 15:12
  • There are multiple examples of wrapping baseQuery in the docs. [For example this](https://redux-toolkit.js.org/rtk-query/usage/customizing-queries#adding-meta-information-to-queries). baseQuery is just a function that either returns `{ data: ... }` or `{ error: ... }`. So you can write your own function that calls your normal baseQuery with all arguments, and then add extra logic with the result of that before finally returning an error or data. – phry Mar 09 '22 at 20:28

0 Answers0