[Vuejs]-I'm not getting the errors back using axios

1πŸ‘

βœ…

   try {
        $fields = $request->validate([
            'product_name' => 'required',
            'product_cost' => 'required | integer',
            'product_selling' => 'required | integer',
            'product_stock' => 'required | integer',]);
    } catch (ValidationException $ex) {
        return response()->json(['errors' =>$ex->errors()], 422); //what ever error format that you desire
    }

You should try catch the $request->validate and send the custom response back, if you are sending API request.

But I don’t suggest using $request->validateAPI requests you can go for Form Request or Validator::make() for more flexibility for api requests too and also you can catch the validation exception in the Handler.php file too and handle it there.

There are lots of articles already posted so try to research "Larvel Validation for API requests".
(I am not gonna link to the specific articles you can find Youtube videos Medium articles to blog posts)

It will help you to understand the Laravel validation process as a whole.

πŸ‘€Anuj Shrestha

1πŸ‘

Did you try with:

error.response.data.error

or just log error and see what is the structure

1πŸ‘

The returned error in Axios not always has the error.response property. You probably should structure your error handling as in this example in the Axios docs.

Hope this helps.

πŸ‘€Gabe

Leave a comment