masmerise / livewire-toaster

Beautiful toast notifications for Laravel / Livewire.
MIT License
395 stars 23 forks source link

Redirect with toast does not work in handler render() #25

Closed Xety closed 1 year ago

Xety commented 1 year ago

Hello,

In my handler.php i have the following :

<?php

namespace BDS\Exceptions;

use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Response;
use Illuminate\Session\TokenMismatchException;
use Illuminate\Support\Str;
use Throwable;

class Handler extends ExceptionHandler
{
    /**
     * The list of the inputs that are never flashed to the session on validation exceptions.
     *
     * @var array<int, string>
     */
    protected $dontFlash = [
        'current_password',
        'password',
        'password_confirmation',
    ];

    /**
     * Render an exception into an HTTP response.
     * 
     * @param $request
     * @param Throwable $e
     * @return JsonResponse|RedirectResponse|Response|\Symfony\Component\HttpFoundation\Response
     * @throws Throwable
     */
    public function render($request, Throwable $e)
    {
        if ($e instanceof ModelNotFoundException) {
            // TOAST NOT WORKING 👈
            return back()->error("L'enregistrement n'existe pas ou a été supprimé !");

            // TOAST WORKING 👈
            return back()->with('toasts', [
                [
                    'type' => 'error',
                    'duration' => 3000,
                    'message' =>"L'enregistrement n'existe pas ou a été supprimé !"
                ]
            ]);
        }

        return parent::render($request, $e);
    }

    /**
     * Register the exception handling callbacks for the application.
     */
    public function register(): void
    {
        $this->renderable(function (\Exception $e) {
            // Error 419 csrf token expiration error
            // TOAST WORKING 👈
            if ($e->getPrevious() instanceof TokenMismatchException) {
                return back()->error("Vous avez mis trop de temps à valider le formulaire ! C'est l'heure de prendre un café !");
            };

            // Error 403 Access unauthorized
            // TOAST WORKING 👈
            if ($e->getPrevious() instanceof AuthorizationException) {
                return back()->error("Vous n'avez pas l'autorisation d'accéder à cette page !");
            }
        });
    }
}

All toast in the register() function perfectly, (403 & 419 errors), but in the render() function it does not work. I have to do this to get it work :

return back()->with('toasts', [
    [
        'type' => 'error',
        'duration' => 3000,
        'message' =>"L'enregistrement n'existe pas ou a été supprimé !"
    ]
]);

Is that a normal behavior ? If yes, maybe add this to the Readme, since there's no infos about flashing it with session. Anyways, good work on this package, very usefull with livewire.

mabdullahsari commented 1 year ago

Hey, have you tried looking at similar issues first?

e.g. https://github.com/masmerise/livewire-toaster/issues/4

It ended up being in conflict with another package. Are you sure it is not the case here too?