1

I'm making a script to fetch some data from my api:

const success = (response) => {
  console.log(response);
};

const failed = (error) => {
  console.log(error);
};

axios.$http.get('/somedata')
  .then((response) => {
    success(response.data);
  })
  .catch((error) => {
    failed(error);
  });

/somepage is a non-existing page so it returns a 404. But the catch is not handling this. Why not? In my console I have the error TypeError: Cannot read property 'data' of undefined. Why does it not run the failed() function? I don't understand.

Jordy
  • 4,719
  • 11
  • 47
  • 81

3 Answers3

1

Found out it was related to a custom interceptor handling 401-errors (but not 404 errors)...

Jordy
  • 4,719
  • 11
  • 47
  • 81
  • Presumably you mean "... a custom interceptor, which is designed to handle 401-errors and which *should* re-throw all others ... but doesn't"? – Roamer-1888 Dec 03 '16 at 06:53
0

Judging by the error message, it looks like "success(response.data);" is being called. Is it possible the server is successfully returning a page that says something like "Error 404" rather than actually returning http response code 404?

Rocky Sims
  • 3,523
  • 1
  • 14
  • 19
0

You could impliment a check for 404s.

axios.$http.get('/somedata')
  .then(response => {
    if(response.status !== 404) //or any status code really
        success(response.data);
    else
        failed(response)
  })
  .catch((error) => {
    failed(error);
  });

Then again what you probably want to check for is to make sure it's a 200 that returns.

axios.$http.get('/somedata')
  .then(response => {
    if(response.status === 200)
        success(response.data);
    else
        failed(response)
  })
  .catch((error) => {
    failed(error);
  });
devilfart
  • 351
  • 1
  • 5