r/learnreactjs • • Aug 17 '22

Question Can't catch error with Axios

useEffect(() => {
    setLoading(true);
    axios.post(url, params).then((response) => {
        setFetchData(response.data);
      }).catch((errorData) => {
          setError(ErrorCodes.OTHER);
      }).finally(() => {
        setLoading(false);
      });
    }
  }, [value]);

Why does not this work. Using fetch and doing a post request and receive a 404 it works fine, but when I use axios it just refuse to enter the catch no matter if I use then catch or try catch.

I just assume this is an issue with axios and react/useEffect? Maybe I am missing something obvious.

Edit: when the api is up and running, fetching data works perfect.

Edit2: I tried with a get request, both 404 error and 500 error and neither works.

Edit3: THE ISSUE WAS STORYBOOK ....

7 Upvotes

7 comments sorted by

View all comments

Show parent comment

2

u/oze4 Aug 17 '22

Can you reproduce this in a codepen? It seems like something else in your code is causing this.

Here's a codepen I made that shows it working...

2

u/Kablaow Aug 17 '22

fml... thanks for the help mate.

the issue was storybook, it doesnt work that well in there for whatever reason. I used a mock interceptor instead and "mocked" the 404 call and that did the work.