'Why is the response data going to the catch block after await in Axios?

err.response.data giving back the data I am looking for !! Status code 302 The data is captured in the catch block instead of try block This is the reudx slice code

  export const getAllProject = async (dispatch) => {
  try {
    console.log("before");
    const res = await apiLink.get(`/getAllProjects`);

    console.log(res.data);
    console.log("after");
    dispatch(getAllProjectSuccess(res.data));
  } catch (error) {
    console.log(error.response.data);
    //   toast.error(error.response?.data.message);
  }
};

The API is calling and the data is also coming. I inspected the network tab And also working in postman But after the await, the code is not running. React router version 5 is used in this project.

this is the proof of API calling and using axios interceptor like this

apiLink.interceptors.request.use(
  async (config) => {
    const TOKEN = JSON.parse(localStorage.getItem("accessToken"));

    config.headers["authorization"] = "Bearer " + TOKEN;

    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

apiLink.interceptors.response.use(
  function (response) {
    return response;
  },
  function (err) {
    if (err.response?.status === 500) {
      userLogout();
    }

    return Promise.reject(err);
  }
);

export default apiLink;

Edited:

err.response.data giving back the data I am looking for !! I just log that and the status code of receiving the data is 302 .



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source