Development - Errors Handling


Debug Mode

Every application, built on ApPHP Framework can run in debug mode, as determined by the value of the constant APPHP_MODE. By default, this constant value equals 'production' mode. To run in debug mode, define this constant as 'debug' before including the Apphp.php file. Remember, that running the application in debug mode is less efficient because it keeps many internal logs, but on other other hand, debug mode is also more helpful during the development stage because it provides useful debugging information that will help you easy find and handle errors when occur.

ApPHP MVC Framework - debug panel

You may also use CDebug core class to put the debug text on the debug console. It's useful for "on fly" debugging. The debug text will be shown in the tag General of debug console:
<?php
CDebug::write($test);
CDebug::write($var, 'testing variable');
Or to show it immediately on the screen:
<?php
CDebug::dump($test);
CDebug::dump($var, 1);
// Another option: d() is alias of dump()
CDebug::d($test); 
Another possible solution is to log variables, arrays or objexts into debug console (available when APPHP_MODE = debug):
<?php
$myText = 'my text';
$myArray = array('a'=>'Apple', 'b'=>'Banan', 'c'=>array('1'='111', '2'=>'222'));
CDebug::console($myText);
CDebug::console($myArray);
CDebug::console($myText, 'Test variable');
// Another option: c() is alias of console()
CDebug::c($myText); 
Also, it's possible to print the last query. You may do it in the following way:
<?php
echo YourModel::model()->lastQuery();
echo CDatabase::init()->lastQuery();


Missing Controller and Action

ApPHP Framework allows using a controller to handle the error display work. By default if there is a missing controller the framework will try to call an ErrorController, so you have include such controller into your application. If there missing action event occurs the system will try to call errorAction() method, that define in base CController class, so if there is no errorAction() defined in current controller the base class method will be used to display an error.

Here the example of ErrorController class:
<?php
class ErrorController extends CController
{    
    public function indexAction($code = '404')
    {
        if(in_array($code, array('404', '500'))){
            $redirectCode = $code;
        }else{
            $redirectCode = 'index';
        }
        $this->_view->render('error/'.$redirectCode);                
    }       
}

Creating Custom error handlers

As it was described in previous chapter, you may define your own error handlers. It means you may define errorAction() in controller and then create appropriate view file for it. In this case your own handler will be used instead of the handler of ApPHP Framework.