score:0

you are trying to catch the promise.reject and wrapping it in a try...catch. only one of this will work.

you can either catch a promise rejection or wrap the promise in a try...catch and throw a new error on promise rejection and catch it in catch block.

try this code

const url = five_day_forecast_url.replace("{0}", city);

axios.interceptors.response.use(function (response) {
  return response;
}, function (error) {
   return promise.reject(error);
});

try {
  const request = axios.get(`${url}`)
  .then(
    response => {
      debugger;
    })
    .catch(
    e => {
      debugger;
      throw e // throw the error and catch it
    });
    debugger;
  return {
    type: fetch_five_day_forecast,
    payload: request
  };
} catch {
  debugger;
  // now you can catch the error in catch block
  console.log("error!");
}

// or you can't write async/await code
// make the the function is marked as `async`

try {
  const response = await axios.get(`${url}`)
  return {
    type: fetch_five_day_forecast,
    payload: response
  };
} catch (e) {
  console.error("error happened during fetch");
  console.error(e);
}

score:3

when using try...catch with axios you explicitly have to state the error response like so

catch(error) {
  console.log('[error]', error.response);
  // use the error.response object for some logic here if you'd like
}

otherwise it just returns the string value.

with that response object you can then utilize some logic to do something based on the particular error. more info can be found here https://github.com/axios/axios/issues/960

i hope this helps.


Related Query

More Query from same tag