[Vuejs]-Avoid handling if error has response in Vue with Axios in every request

0👍

response can be treated as optional, because it actually is:

this.validateField(error.response?.data)

Or normalized error that contains needed properties and doesn’t rely on the structure of Axios error can be created in an interceptor:

function (rawError) {
  const error = new Error(error.response?.data?.myError || error.message);
  error.data = error.response?.data || null;
  error.headers = error.response?.headers || null;
  error.status = error.response?.status || 0;
  return Promise.reject(error);
}

Leave a comment