Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dascorp's avatar
Level 26

Handling 404 on axios call in mounted

What would be the best way to handle 404 with vue router on axios callin the mounted of the component loaded with vue router? Shall I simply redirect in catch statement?

0 likes
2 replies
Robstar's avatar

Depends upon the context really. I've always liked axios interceptors, as you can cool things like:

axios.interceptors.response.use((response) => {
    return response;
}, function (error) {
    // Do something with response error
    if (error.response.status === 401) {
        console.log('unauthorized, logging out ...');
        auth.logout();
        router.replace('/auth/login');
    }
    return Promise.reject(error.response);
});
dascorp's avatar
Level 26

tried to put this in app.js

window.axios.interceptors.response.use((response) => {
    return response;
}, function (error) {
    if (error.response.status === 404) {
        console.log('error')
        this.$router.push({
            name: '404'
        });
    }
    return Promise.reject(error.response);
});

it logs to console however does not redirect, highly likely I set up router wrong way here.

Please or to participate in this conversation.