The examples on this site are currently tested to work on Phalcon V3.4 and Phalcon Devtools V3.2 Some issues may arise when using later versions.

Please get in touch or post a comment below the post if you encounter a problem.

Exception handling allows developers to "catch" errors as they occur and handle them before returning the program to its normal working routine. This will allow the program to continue operating rather than crashing the application. In the context of a web application it will allow us to deliver a more positive user experience as it allows us to control what the user sees when something goes wrong.

In the previous post we created an ErrorController to handle errors generated in the SecurityPlugin. In this post we will extend this to handle additional errors and exceptions.

The following code has been taken from the Phalcon\Invo appilcation and should be saved as app/plugins/NotFoundPlugin.php

<?php
use Phalcon\Events\Event;
use Phalcon\Mvc\User\Plugin;
use Phalcon\Dispatcher;
use Phalcon\Mvc\Dispatcher\Exception as DispatcherException;
use Phalcon\Mvc\Dispatcher as MvcDispatcher;
/**
 * NotFoundPlugin
 *
 * Handles not-found controller/actions
 */
class NotFoundPlugin extends Plugin
{
    /**
     * This action is executed before perform any action in the application
     *
     * @param Event $event
     * @param MvcDispatcher $dispatcher
     * @param \Exception $exception
     * @return boolean
     */
    public function beforeException(Event $event, MvcDispatcher $dispatcher, \Exception $exception)
    {
        error_log($exception->getMessage() . PHP_EOL . $exception->getTraceAsString());
        if ($exception instanceof DispatcherException) {
            switch ($exception->getCode()) {
                case Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
                case Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
                    $dispatcher->forward(
                        [
                            'controller' => 'errors',
                            'action'     => 'show404'
                        ]
                    );
                    return false;
            }
        }
        $dispatcher->forward(
            [
                'controller' => 'errors',
                'action'     => 'show500'
            ]
        );
        return false;
    }
}
?>

Next we need to modify the code we added to the app/config/services.php file in Step 2 of "Putting the Access Control List in the Database". Open the file app/config/services.php and un-comment the line which attaches the NotFoundPlugin to the eventsManager.

In the case of specific exception to do with a user trying to access a resource which does not exist the NotFoundPlugin will call the show404Action() of the ErrorsController but in the case of a general unexplained exception the plugin will call the show500Action() which does not yet exist. Edit app/controllers/ErrorsController.php and add an empty show500Action() function as follows:

public function show500Action()
{

}

Now add the following code for the corresponding view in app/views/errors/show500.volt

{{ content() }}
<div class="jumbotron">
    <h1>Internal Error</h1>
    <p>Something went wrong, please contact the website administrator</p>
    <p>{{ link_to('index', 'Home', 'class': 'btn btn-primary') }}</p>
</div>

The ErrorsController will now handle more general exceptions which may occur and if you want to change what the user sees you need only edit the appropriate view.