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

Handling 500 error

Hello every1!

So, the problem is - How do I handle 500 error on phalcon. I mean - I got clear, how to handle not found errors

Code:

$eventsManager->attach( 'dispatch:beforeException', function($event, $dispatcher, $exception){ switch ($exception->getCode()){ case Dispatcher::EXCEPTION_HANDLER_NOT_FOUND: case Dispatcher::EXCEPTION_ACTION_NOT_FOUND: $dispatcher->forward( array( 'controller' => 'error', 'action' => 'notFound', ) ); return false; break;

    default:
      $dispatcher->forward(
        array(
          'controller' => 'error',
          'action' => 'uncaughtException',
        )
      );
      return false;
    break;
  }
}

);

But the part with 500 error is not working. Any suggestions?

What is your phalcon version ?



9.3k

Currently 1.2.3

i'm in 1.2.3 and I got the same probleme with version 1.2.3 too.

It should be fixed with 1.2.1 : https://github.com/phalcon/cphalcon/issues/140

But this issue on github relates the same statement with 1.3.0 : https://github.com/phalcon/cphalcon/issues/1763

Looks like it's not really fixed right now.

In the interim you may use the php register_shutdown_function.

I'm still looking for an eventual fix



9.3k

Ok by now.

Mby you know how to forward to my action from /public/index.php file?

Then I could use forward to exception action from try{}catch(){} block.

edited May '14

Actually the dispatch event doesnt take 500 errors. But it already takes exceptions.

Here is a sample of my dispatch event working for 404 and exceptions :

    <?php
    $eventsManager = new \Phalcon\Events\Manager();
        $eventsManager->attach("dispatch:beforeException", function($event, $dispatcher, $exception) {

            //Handle 404 exceptions
            if ($exception instanceof DispatchException) {
                $dispatcher->forward(array(
                    'controller' => 'error',
                    'action' => 'notFound'
                ));
                return false;
            }

            // handles exceptions
            $dispatcher->forward(array(
                'controller' => 'error',
                'action' => 'fatal'
            ));

            return false;
        });

        $dispatcher->setEventsManager($eventsManager);

If you use the try / catch in index.php. Then you cant forward when you fall in the catch clause, because the dispatch loop would be ended. Forward needs a running dispatch loop. That's why we use the event.

Please let me know if everything is ok for you