2

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!

3
  • Please look at MODIFIED # 1 Commented Dec 1, 2021 at 21:13
  • What does your browser's network inspector tell you about the response? Does it contain the expected values and HTTP response code? Commented Dec 1, 2021 at 22:18
  • What I see in browser's console : prnt.sc/21cbxl9 on error Commented Dec 2, 2021 at 4:33

1 Answer 1

1

The onError() callback is only called when there is a specific flag in the session called errors. So, in you server's catch block you need to flash the errors in the user's session and redirect to your calling page:

try {
  // errors throw here
} catch {
   return back()->with('errors', 'Error adding ad');
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.