4

I was wondering, what's the best way to handle exceptions in Phalcon? I'd like to create a default error page for when an error occurs. So I rewrote /app/public/index.html to this:

<?php

error_reporting(E_ALL);

try {
    /**
     * Define some useful constants
     */
    define('BASE_DIR', dirname(__DIR__));
    define('APP_DIR', BASE_DIR . '/app');

    require_once __DIR__ . '/../vendor/autoload.php';


    /**
     * Read the configuration
     */
    $config = include APP_DIR . '/config/config.php';

    /**
     * Read auto-loader
     */
    include APP_DIR . '/config/loader.php';

    /**
     * Read services
     */
    include APP_DIR . '/config/services.php';

    /**
     * Handle the request
     */
    $application = new \Phalcon\Mvc\Application($di);

    echo $application->handle()->getContent();
} catch (Exception $e) {
    echo 'This is where I want my handling to be';
}

However, when an error gets thrown, I keep getting the default Chrome 500 error window. The error is logged to OS X's error console, but I'm not seeing my echo. What am I doing wrong?

user4992124
  • 1,574
  • 1
  • 17
  • 35
  • This is the correct way to bootstrap your app and catch exceptions. **What kind of errors you want to catch?** For example if you change db password to incorrect one your code will work. But if you make a parse error it will not be catched. – Nikolay Mihaylov Mar 02 '16 at 07:25

3 Answers3

3

Use multiple catch block instead of just \Exception add specific type of exceptions like \PDOException

try
{
 /* something */
}
catch(\Exception $e )
{
   handler1( $e );
}
catch ( \PDOException $b )
{
   handler2( $e );
}
// add more ex here

You said "when an error occurs", if you want to handle error then add error handler in the top of Phalcon bootstrap (public/index.php) file.

function handleError($errno, $errstr) {
    echo "<b>Error:</b> [$errno] $errstr<br>";
    //do what ever
    die();
}
set_error_handler("handleError");
M Ashraful A
  • 627
  • 6
  • 13
1

in app/config/service.php

use \Phalcon\Mvc\Dispatcher as PhDispatcher;

.
.
.


$di->set(
'dispatcher',
function() use ($di) {

    $evManager = $di->getShared('eventsManager');

    $evManager->attach(
        "dispatch:beforeException",
        function($event, $dispatcher, $exception)
        {
            switch ($exception->getCode()) {
                case PhDispatcher::EXCEPTION_HANDLER_NOT_FOUND:
                case PhDispatcher::EXCEPTION_ACTION_NOT_FOUND:

                    $dispatcher->forward(
                        array(
                            'namespace' => 'App\Controllers\Web',
                            'controller' => 'error',
                            'action'     => 'show404',
                        )
                    );
                    return false;
            }
        }
    );
    $dispatcher = new PhDispatcher();
    $dispatcher->setEventsManager($evManager);
    return $dispatcher;
},
true

);

0

If you want to show the PHP parse errors you need to alter this line in you PHP.ini file:

display_errors = on

You might need to restart your webserver for this change to take effect.


If you are unsure where the ini file is located, output the following line of code:

<?php phpinfo(INFO_GENERAL) ?>

This should display the location of the PHP.ini file


On an other note. Catching your errors like that is not a good practise. Phalcon provides different ways to catch errors.

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

Refer to the Phalcon INVO repository for the full example.

Timothy
  • 2,004
  • 3
  • 23
  • 29