How to handle error response ?
afifnandya opened this issue · comments
Afif Nandya S commented
in the json api example
HTTP/1.1 422 Unprocessable Entity
Content-Type: application/vnd.api+json
{
"errors": [
{
"status": "422",
"source": { "pointer": "/data/attributes/firstName" },
"title": "Invalid Attribute",
"detail": "First name must contain at least three characters."
}
]
}
i don't see how to handle error in the documentaion, thanks
Matthew Richardson commented
The library makes no attempt to intercept API errors, so they will be raised 'directly' from axiosYou can use the then/catch
mechanism to handle them. the axios error handling docs are here:
https://github.com/axios/axios#handling-errors
For jsonapi-vuex, it would look something like:
this.$store
.dispatch('jv/get', '/nosuchendpoint')
.then((res) => {
// request is successful - res = normalised jsonapi
console.log(res)
})
.catch((errs) => {
// request fails
if (errs.response) {
// Work with each error from the 'errors' array
for (let err of errs.response.data.errors) {
console.log(err.detail)
} else {
// Handle non-request errors
console.log(errors.message)
})
I'll see about updating the docs to explain how errors are handled.
Afif Nandya S commented
ok if that so, thanks for replying