Moved backend error handling in axios

This commit is contained in:
Eliot Berriot 2018-03-03 12:38:11 +01:00
parent f9786d4b45
commit 2c79418d81
No known key found for this signature in database
GPG key ID: DD6965E2476E5C27
2 changed files with 18 additions and 16 deletions

View file

@ -47,11 +47,28 @@ axios.interceptors.request.use(function (config) {
axios.interceptors.response.use(function (response) {
return response
}, function (error) {
error.backendErrors = []
if (error.response.status === 401) {
store.commit('auth/authenticated', false)
logger.default.warn('Received 401 response from API, redirecting to login form')
router.push({name: 'login', query: {next: router.currentRoute.fullPath}})
}
if (error.response.status === 404) {
error.backendErrors.push('Resource not found')
} else if (error.response.status === 500) {
error.backendErrors.push('A server error occured')
} else if (error.response.data) {
for (var field in error.response.data) {
if (error.response.data.hasOwnProperty(field)) {
error.response.data[field].forEach(e => {
error.backendErrors.push(e)
})
}
}
}
if (error.backendErrors.length === 0) {
error.backendErrors.push('An unknown error occured, ensure your are connected to the internet and your funkwhale instance is up and running')
}
// Do something with response error
return Promise.reject(error)
})