We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Heading Exceptions via Events

Hello, i have problem with classic \Exception.

My code:

$di->set(
    'dispatcher',
    function () {
        $dispatcher = new Dispatcher();

        $eventsManager = new EventsManager();

        $eventsManager->attach(
            'dispatch:beforeException',
            new DispatcherLisener()
        );

        $dispatcher->setEventsManager($eventsManager);

        return $dispatcher;
    }
);
class DispatcherLisener extends \Phalcon\Mvc\User\Component
{
    public function beforeException(\Phalcon\Events\Event $event, \Phalcon\Mvc\Dispatcher $dispatcher, \Exception $exception): bool
    {
        if ($exception instanceof \Phalcon\Mvc\Dispatcher\Exception) {
            $this->response->redirect('misc/error/show404');
        } else {
            $this->response->redirect('misc/error/show500');
        }

        return false;
    }
}
class AccountController extends \Phalcon\Mvc\Controller
{
    public function listAction()
    {
        throw new \Exception();
    }
}

404 work fine, but i cant catch 500 exception. Any sugestions?

edited Nov '16

Ok... this code work. I make mistake.

Can i catch error like this?

class AccountController extends \Phalcon\Mvc\Controller
{
    public function listAction()
    {
        $x = new \This\Class\Dont\Exist();
    }
}