I have an api that I use to check the weather, but it may return different codes depending on the situation. In my axios.get in the catch block I want to redirect the user to an error page depending on the code returned by the server. How can this be done? I tried code below but it works wrong. When an error occurs, first the 'cityDoesntExist' page appears and then almost immediately the 'serverDoesntWork' page
methods: {
async getWeather() {
const cityName = this.$route.params.cityName;
await axios.get(`/api/weather`, {params: { cityName: cityName }})
.then(response => response.data)
.then(data => this.weather = data)
.catch(function (error) {
if(error.response.status === 404) {
router.push({name: 'cityDoesntExist'});
}
if(error.response.status === 500) {
router.push({name: 'serverDoesntWork'});
}
});
},
},