[Vuejs]-How to catch all the Laravel API errors with Axios and VueJS?

0👍

for catch all errors in laravel you need to edit App\Exceptions\Handler file like this

 public function render($request, Exception $exception)
{
    if ($exception){
        return response()->json([
            'status' => 'failed',
            'error' => $exception->getMessage()
        ]);
   }
    return parent::render($request, $exception); // return json with status => 'success' and 'message' => 'Done!'
}

then in your Vuejs project easily you can find out exceptions that have occurred in the backend side

axios.get('/usuarios').then(function(response){
   if(response.data.status == 'success'){
      Swal.fire('OK',response.data.message,'success') // or call another component
   }else{
      Swal.fire('Error',response.data.error,'error')   
   }
})

Leave a comment