Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Rafaelindriago's avatar

Someone knows how the new "exception" hook works?

Hey guys, i want to know if someone knows how the new added hook "exception" works on Livewire, because when i try to used it, the hook has unexpected behaviour.

We can use the Exception type on the parameters list for the first argument?

0 likes
1 reply
Rafaelindriago's avatar
Rafaelindriago
OP
Best Answer
Level 1

Using this snippet:

    public function exception($exception, $stopPropagation): void
    {
        dd($exception);

        if ($exception instanceof ModelNotFoundException) {
            $this->forceClose();

            $stopPropagation();
        }
    }

If i throw a ModelNotFoundException i got an empty black modal on browser (Like the default modal for 404 but with black background).

With this snippet, i got the dump of the Exception with this type instead of ModelNotFoundException:

    public function exception(Exception $exception, $stopPropagation): void
    {
        dd($exception);

        if ($exception instanceof ModelNotFoundException) {
            $this->forceClose();

            $stopPropagation();
        }
    }
Exception {#1807 ▼ // app\Livewire\Traits
  #message: ""
  #code: 0
  #file: "
vendor\laravel\framework\
src\Illuminate\Container\Container.php"
  #line: 944
  trace: {▶}
}

And with this snippet, i got the dump of ModelNotFoundException:

    public function exception(ModelNotFoundException $exception, $stopPropagation): void
    {
        dd($exception);

        if ($exception instanceof ModelNotFoundException) {
            $this->forceClose();

            $stopPropagation();
        }
    }
Illuminate\Database\Eloquent\ModelNotFoundException {#1807 ▼ // app\Livewire\Traits
  #message: ""
  #code: 0
  #file: "
vendor\laravel\framework\
src\Illuminate\Container\Container.php"
  #line: 944
  #model: null
  #ids: null
  trace: {▶}
}

But this makes no sense, because the ValidationException is catched too and handled like ModelNotFoundException. 🤯

And finally i found a "solution", is a mandatory to use the exactly signature from the docs. 😒

This snippet:

    public function exception($e, $stopPropagation): void
    {
        if ($e instanceof ModelNotFoundException) {
            $this->forceClose();

            $stopPropagation();
        }
    }

If the $e is changed to another word, like $exception, the hook doesn't work as expected. 😪

Hope this can help other devs. 🤓

Please or to participate in this conversation.