In my project, I utilize axios interceptors to manage various errors, particularly those without a response. However, I rely on the error.response.data message for validations and displaying messages stored in the backend in certain areas of the project. Despite this interceptor, I still need to check if the error has a response.
This is how my interceptor is set up:
axios.interceptors.response.use(
function (response) {
...
},
function (error) {
if (!error.response) {
...
return Promise.reject(new Error(error.message))
}
An instance where I specifically need error.response for a request is:
this.$store.dispatch('updateField', { [this.fieldKey]: this.value ? this.value : null }).catch((error) => {
this.validateField(error.response.data)
})
To prevent an error in the console, I am forced to place the validateField call inside an if(error.response)
. Is there a better solution than scattering this conditional throughout my code?