In laravel 8 / inertiajs 0.10 / vue 3 I want to catch some error which happens on server, like :
this.form.post(this.route('ads.store'), {
preserveScroll: true,
onSuccess: (p) => { // On succdess I see this message
console.log('onSuccess p::')
console.log(p)
Swal.fire(
'New Ad',
'Your post has successfully been published!',
'success'
)
this.form.description = null
},
onError: (p) => { // That is not triggered!
console.log('onError p::')
console.log(p)
}
})
In control :
public function store( AdFormRequest $request) {
$data = $request->all();
$data['status']= 'A';
$ad = Ad::create($data);
return $request->wantsJson()
? new JsonResponse($ad, 200)
: back()->with('status', 'Error saving add');
}
So if one of required fields is empty I got laravel error popup window... How to catch it and to show it in Swal.fire ?
MODIFIED # 1:
Searching in net I found onError property, but making :
this.deleteForm.delete(this.route('ads.destroy', this.nextAd), {
preserveScroll: true,
onError: (p) => { // THIS PART IS NOT CALLED ON ERROR
console.log('onError p::')
console.log(p)
Swal.fire(
'Delete Ad',
'Error deleting ad!',
'error'
)
},
onSuccess:()=>{
Swal.fire( // THIS PART IS CALLED ON SUCCESS
'Delete Ad',
'Your post has successfully been Delete!',
'success'
)
}
})
and in control :
public function destroy(Request $request, Ad $ad) {
try {
DB::beginTransaction();
$ad->deleTTTte();
DB::commit();
} catch (QueryException $e) {
DB::rollBack(); // I SEE THIS MESSAGE IN LOG FILE ON ERROR
\Log::info( '-1 AdController store $e->getMessage() ::' . print_r( $e->getMessage(), true ) );
return $request->wantsJson()
? new JsonResponse($ad, 500 /*HTTP_RESPONSE_INTERNAL_SERVER_ERROR*/ )
: back()->with('status', 'Error adding ad : ' . $e->getMessage());
return;
}
return $request->wantsJson()
? new JsonResponse($ad, HTTP_RESPONSE_OK)
: back()->with('status', 'Ad saved succesully');
}
Which way is correct?
Thanks!