Skip to content

Instantly share code, notes, and snippets.

@allaniftrue
Created May 27, 2018 12:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save allaniftrue/67d78a0c18ab7610e69687f06d998903 to your computer and use it in GitHub Desktop.
Save allaniftrue/67d78a0c18ab7610e69687f06d998903 to your computer and use it in GitHub Desktop.
Snippet to capture laravel's 422 error
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