mstdmstd
mstdmstd

Reputation: 3071

How can I catch 404 error on server with axios request?

In vue/cli 4 / vuejs 2.6.10 / vuex,3.0.1 app I make axios request and when I got 404 error from server I want to catch it, like:

this.credentialsConfig.headers.Authorization = 'Bearer ' + this.currentLoggedUserToken
axios.get(this.apiUrl + '/adminarea/categories/' + this.category_id, this.credentialsConfig)
    .then(({ data }) => {
        this.categoryRow = data.categories[0]
        this.is_page_loaded = true
    })
    .catch(error => {
        console.log('error::')
        console.log(error)
        console.log('error.status::')
        console.log(error.status) // NO VALUE

        debugger
        if (error.status == 404) { // tHAT DOES NOT WORK
            this.showPopupMessage("Categories editor", "User # "+this.category_id+" not found !", 'warn');
            this.$router.push({path: '/admin/categories'}); // TO MOVE TO OTHER ROUT!
        }

        console.error(error)
        this.showPopupMessage('Categories Editor', error.response.data.message, 'warn')
        this.is_page_loaded = true
    })

But checking error var I do not see any structure with error code : https://i.sstatic.net/xd3VH.jpg

How can I get error code here ?

"axios": "^0.19.0",
"vue": "^2.6.10",
"vue-avatar": "^2.1.8",
"vuex": "^3.0.1"

Thanks!

Upvotes: 1

Views: 1877

Answers (1)

KingKabyle
KingKabyle

Reputation: 381

Instead of using error.status use error.response.status. Try this. And if this don't fit you can use console.dir(error) to display the error object.

Upvotes: 4

Related Questions