Moved backend error handling in axios

merge-requests/154/head
Eliot Berriot 2018-03-03 12:38:11 +01:00
rodzic f9786d4b45
commit 2c79418d81
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: DD6965E2476E5C27
2 zmienionych plików z 18 dodań i 16 usunięć

Wyświetl plik

@ -100,24 +100,9 @@ export default {
username: this.username
}})
}, error => {
self.errors = this.getErrors(error.response)
self.errors = error.backendErrors
self.isLoading = false
})
},
getErrors (response) {
let errors = []
if (response.status !== 400) {
errors.push('An unknown error occured, ensure your are connected to the internet and your funkwhale instance is up and running')
return errors
}
for (var field in response.data) {
if (response.data.hasOwnProperty(field)) {
response.data[field].forEach(e => {
errors.push(e)
})
}
}
return errors
}
},
computed: {

Wyświetl plik

@ -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)
})