How To Set Erroraction For Yiic Commands?

I’m trying to have a custom exception handler for Yii console commands (i.e. Yiic).

In my console config, I have configured the CErrorHandler::errorAction property to be a specific route:




// Specify our own route for the error action (so we can tell newrelic about our exceptions)

'errorHandler'=>array(

    'errorAction'=>'site/error',

),



I have a test command:


<?php

class JunkCommand extends CConsoleCommand

{

    public function actionIndex()

    {

        $i = 10/0; // generates a  Division by zero error

    }

}

The exception does not trigger my custom error action. Output:




$ ./yiic junk

PHP Error[2]: Division by zero

    in file /var/www/pph/commands/JunkCommand.php at line 24

#0 unknown(0): JunkCommand->actionIndex()

#1 /var/www/pph/lib/pph-yii-repo/framework/console/CConsoleCommand.php(172): ReflectionMethod->invokeArgs()

#2 /var/www/pph/lib/pph-yii-repo/framework/console/CConsoleCommandRunner.php(67): JunkCommand->run()

#3 /var/www/pph/lib/pph-yii-repo/framework/console/CConsoleApplication.php(91): CConsoleCommandRunner->run()

#4 /var/www/pph/lib/pph-yii-repo/framework/base/CApplication.php(169): CConsoleApplication->processRequest()

#5 /var/www/pph/lib/pph-yii-repo/framework/yiic.php(33): CConsoleApplication->run()

#6 /var/www/pph/yiic.php(23): require_once()

#7 /var/www/pph/yiic(4): require_once()




Is it possible to use the errorHandler component with yiic to do custom error processing?

There may be a better way, but one way is to extend CErrorHandler and implement the handleException ourselves. e.g.




// Specify our own route for the error action (so we can tell newrelic about our exceptions)

'errorHandler'=>array(

    'class'=>'ErrorHandler',

    'errorAction'=>'site/errors',

),






<?php

class ErrorHandler extends CErrorHandler

{

    /**

     * Handles the exception.

     * @param Exception $exception the exception captured

     */

    protected function handleException($exception)

    {

        $app=Yii::app();


        if ($app instanceof CConsoleApplication) {

            echo "TOMNOTE from inside ".__METHOD__."\n";

            // Do stuff here

        }


        parent::handleException($exception);

    }


    /**

     * Handles the PHP error.

     * @param CErrorEvent $event the PHP error event

     */

    protected function handleError($event)

    {

        $app=Yii::app();


        if ($app instanceof CConsoleApplication) {

            echo "TOMNOTE from inside ".__METHOD__."\n";

            // Do stuff here

        }


        parent::handleError($event);

    }

}