Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Axios Stop 404 error appearing in the console.log

I am checking to see if a resource exists via Axios, it is normal to expect a 404 to be returned if it is not present.

But, when the 404 is returned, it is displayed in the console. I have tried to catch the error, but this does not prevent it from being displayed by chrome.

        axios.get('/api/user/checkin').then((response) => {
            Vue.set(UserStore,'checkin', response.data)
            this.loading = false;
        }).catch((error) =>{})

I am checking to see if the user is checked if they are I will return a record of when they checked in and some details on the site.

If they are not then I have nothing to return so I am returning a 404. I could return a blank record but that does feel very restful.

like image 332
JaChNo Avatar asked Aug 06 '19 08:08

JaChNo


People also ask

How do I get rid of Axios error?

Update the function in the Search component inside Search. js. This approach works to remove the console errors from Axios that return 404 error in the console.

Does Axios throw error on 404?

By default, axios throws an error when it encounters any 4XX / 5XX status code. Adding this line overrides that default behavior. You can then check the status code yourself (in response. status ), conditionally handling the 404 as you need.

Why am I getting Axios error?

error. Note: This occurs when the browser was able to initiate a request but did not receive a valid answer for any reason. Earlier we mentioned that the underlying request Axios uses depends on the environment in which it's being run. This is also the case for the err. request object.

How do I get rid of console errors?

Alternatively you can call console. clear() to remove all existing items within the console. However I wouldn't advise doing either of these. A much better approach would be to fix your codebase so that you avoid errors and implement graceful error handling where unexpected errors may occur.


1 Answers

This is a chrome behavior. see this and also this.
You can do console.clear() in catch as suggested by this answer.

axios.get('/api/user/checkin')
  .then((response) => {
    Vue.set(UserStore,'checkin', response.data)
    this.loading = false;
  })
  .catch(() => console.clear()) // this will clear any console errors caused by this request

or
.catch(console.clear) for short.

But be aware that you will lose any previous console logs.

Edit:

You may want to clear the console when you receive a 404 response only.

axios.get('/api/user/checkin')
  .then((response) => {
     Vue.set(UserStore,'checkin', response.data)
     this.loading = false;
  })
  .catch((error) => {
    if(error.response && error.response.status === 404) {
      console.clear();
    }
  })

Refer to handling errors in Axios for more.

like image 177
Ahmed Ibrahim Mah. Avatar answered Oct 22 '22 01:10

Ahmed Ibrahim Mah.