Yii 1.1: How to customize error handling in console-mode applications

3 followers

One can control the detail of a web application in the webroot/index.php file by manipulating the values of YII_DEBUG and YII_TRACE_LEVEL, but these don't appear to be used by console applications. This is because although the base CApplication class defines a displayError() method that does consider these variable, CConsoleApplication overrides it with a function that doesn't honor them.

This means that console app errors produce huge stacktraces that scroll right by.

This can be easily fixed by setting up a wrapper class to CConsoleApplication that overrides this error handling, and then using this class when instantiating the app.

1) Create a wrapper class for the CConsoleApplication system class:

<?php
// in protected/components/MyConsoleApplication.php
 
class MyConsoleApplication extends CConsoleApplication {
 
    public function displayError($code, $message, $file, $line)
    {
        echo "PHP Error[$code]: $message\n";
        echo "in file $file at line $line\n";
 
        if (YII_DEBUG) debug_print_backtrace();
    }
}

This function was taken directly from CConsoleApplication::displayError, but we've added the conditional test for YII_DEBUG. You can of course customize it in any other way you like, as well as override any other features of CConsoleApplication.

2) Update your console entry script to reflect the new wrapper class.

Normally we use the Yii::createConsoleApplication() method, but all that really does is call new on the class, so we can replace it with our own call. And since Yii isn't quite running yet the autoloader is not in place, so we have to explicitly require_once the wrapper.

I normally put my console wrappers in the protected/exec/ folder because other scripts are found there too. The console entry script now looks like:

#!/usr/bin/php
#
# Script found in protected/exec/myconsole.php
 
<?php
 
$protected = dirname(__FILE__) . '/..';
$yii       = "$protected/../yii/framework/yii.php";   // EDIT THIS TO TASTE
$config    = "$protected/config/console.php";
 
define('YII_DEBUG', false);
 
require_once( $yii );
require_once( "$protected/components/MyConsoleApplication.php" );
 
// Yii::createConsoleApplication($config)->run();    // DELETE THIS
$app = new MyConsoleApplication($config);            // ADD THIS
$app->run();

With this change made, errors from console apps produced detailed backtraces or not depending on how you define YII_DEBUG.

Total 1 comment

#5322 report it
Ciro at 2011/10/04 08:53am
Complete list of command

Hello, following your instructions I noted only the commands implemented by me and not the basic Yii command. I modified the code as follows to get the commands of the system:

<?php
#!/usr/bin/php
$protected = dirname(__FILE__);
$yii       = "$protected/../../yii-trunk/framework/yii.php";   
$config    = "$protected/config/console.php";
define('YII_DEBUG', false);
require_once( $yii );
require_once( "$protected/components/SioConsoleApp.php" );
if(isset($config)){
    $app = new MyConsoleApplication($config);            
    $app->commandRunner->addCommands(YII_PATH.'/cli/commands');
    $env=@getenv('YII_CONSOLE_COMMANDS');
    if(!empty($env))
        $app->commandRunner->addCommands($env);
}
$app->run();

Leave a comment

Please to leave your comment.

Write new article