I am using the following code to post something to Laravel:
upload: async function() {
response = await axios.post(url, data, {
headers: {
'Content-Type': 'multipart/form-data',
}
}).catch(function(error) {
console.log(error.response.data.errors);
});
(Unrelated code omited)
This works overall when Laravel returns 200. If I return 401, 422 or something from Laravel Axios throws an error in the console and the whole method stops running.
I've always considered this a very non-developer-friendly behavior - am I not understanding it? Returning different status codes from your backend API is supposed to help reading the response in Vue, but Vue/Axios treat every non-200 response as a fatal error.
How can I catch the error - do something (and bind $this
to the callback) and then continue with the method without killing the whole script?
axios
is doing what is should do. Do you think a redirect (3xx
) is a successful response from an API ? How about4xx/5xx
? If your code stops working when you get a4xx
, that's because you are doing something wrong, you are usingasync/await
in a bad way, you have to usetry/catch
and notawait + js.catch
... That is why your code stops working... – matiaslauriti