Created
May 27, 2018 12:26
-
-
Save allaniftrue/67d78a0c18ab7610e69687f06d998903 to your computer and use it in GitHub Desktop.
Snippet to capture laravel's 422 error
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
axios.post('/login', { | |
email: self.email, | |
password: self.password | |
}) | |
.then(response => { | |
// successful login | |
}, error => { | |
const responseData = error.response.data; | |
const firstErrorKey = Object.keys(responseData.errors); | |
// console.log('firstErrorKey: ', firstErrorKey); | |
// console.log('firstErrorResponse: ', responseData.errors[firstErrorKey][0]); | |
self.loginError = true; | |
if (error.response.status === 422) { | |
self.error = responseData.errors[firstErrorKey][0] || 'User / Password does not match our record'; | |
} else { | |
self.error = 'An un-expected error occurred please try again later'; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment