Я пытаюсь разобраться, как обрабатывать ошибки при использовании axios. Пожалуйста, смотрите код ниже:
//Admin.vue
methods: {
saveClicked: function(){
updateConfig(this.editedConfig)
.then(response => {
console.log("in here");
console.log(response);
this.info.showAlert = true;
this.info.message = "Successfully updated config!!"
this.info.alertType = "success";
this.showJsonEditor = !this.showJsonEditor;
})
.catch(error => {
this.info.showAlert = true;
this.info.message = "Failed to update config, please try again later"
this.info.alertType = "error";
});
}
}
//network.js
function updateConfig(editedConfig){
return axios.put(URL,editedConfig, {
headers: {
"Content-type": "application/json"
}
})
.then(response => response)
.catch(error => error);
}
Когда запросы успешны, все в порядке. Я получил предупреждение, что все хорошо.
Я заставил свой бэкэнд возвращать ошибку для каждого запроса, чтобы я мог смоделировать поведение ошибки в своем приложении Vue, и вот что я заметил:
Даже при получении ошибки. Программа проходит через then()
в Admin.vue
, проверьте изображение ниже:
![enter image description here](https://i.stack.imgur.com/98Eem.png)
Что я пропустил?