Unable to Retrieve Custom Error Message (Sent from Backend) Using Axios

I’m utilizing Redux Toolkit for state management, and when I make requests with Axios, I encounter an issue where I’m unable to retrieve the custom error message sent by the backend. Although I receive the error code, the specific error message is not captured in the response. This behavior differs from what I observe in Postman, where I can see the custom error message. I’m seeking guidance on how to handle custom error messages from the backend when using Axios with Redux Toolkit.

Here’s an example of my backend code for handling the login logic:

exports.loginUser = asyncErrorCatcher(async (req, res, next) => {
  const { email, password } = req.body;

  if (!email || !password) {
    return next(new ErrorHandler("Please Enter Email & Password", 400));
  }
  const user = await User.findOne({ email }).select("+password");

  if (!user) {
    return next(new ErrorHandler("Invalid username or password", 401));
  }

  const isPassword = await user.verifyPassword(password);

  if (!isPassword) {
    return next(new ErrorHandler("Invalid username or password", 401));
  }

  jwtMssg(user, 200, res);
});

In my userAction.js file, I have the following logic for making the login request:

export const loginUser = createAsyncThunk(
  "user/login",
  async ({ loginEmail, loginPassword }) => {
    console.log(loginEmail, loginPassword);
    const config = { headers: { "Content-Type": "application/json" } };
    const { data } = await axios.post(
      `/VC1/login`,
      { email: loginEmail, password: loginPassword },
      config
    );
    return data;
  }
);

In my userReducer.js file, I attempt to handle the rejected case as follows:

export const userReducer = createReducer(state, (builder) => {
  builder
    .addCase(loginUser.rejected, (state, action) => {
      return {
        ...state,
        loading: false,
        isAuthenticated: false,
        user: null,
        error: action.error,
      };
    })
});

While testing the API in Postman, I receive the expected response for a 401 error code with the custom error message "Invalid username or password.
image

However, when making the same request with Axios, the custom error message is not captured, and I only see the error code. I appreciate any guidance on resolving this issue.