Yii 1.1: pccustomerroractionfilter

Filter for specifying a controller's method as the 'error action handler'
2 followers

PcCustomErrorActionFilter

Features

  • Using the provided filter class you can easily state your temporary 'error handler action method'. This means that in the context of one controller, if you want to consolidate error handling for multiple action methods, you can tell Yii to run a certain action method when an error occurs in those controllers.
  • This is useful in cases, for example, you have a controller that provides JSON data to client side via AJAX. Its logical that on errors on server side Yii will not render the usual error pages but rather a nice error message formatted in JSON (since those are full html while our client here is JS that expects JSON).

Usage

  • Extract the filter class included (PcCustomErrorActionFilter) in this extension under /protected/components.
  • In the controller you wish to integrate this extension, create an action method that will handle the errors. For example (real example shown):
public function actionHandleErrors() {
    // log the error message first.
    $error = Yii::app()->errorHandler->error;
    // traces attribute is huge. Remove it to prevent inflation of log files
    unset($error['traces']);
    Yii::log("Fatal Error occurred! This was the reported error:\n" . 
        var_export($error, true), CLogger::LEVEL_WARNING, __METHOD__);
    // return a uniform 'failure' message to the user's eagerly awaiting rowser.
    echo CJSON::encode(array('status' => 'failure'));
}
  • In the relevant controller's 'filters()' method, you need to add the following configuration:
public function filters() {
    return array(
        array(
            // be sure to update the path and also retain the '- methodName' !
            'application.components.PcCustomErrorActionFilter - handleErrors',
            // this is the 'route' to the error handling method.
            'errorActionRoute' => 'moduleId/controllerId/handleErrors',
        ),
    );
}

Resources

Be the first person to leave a comment

Please to leave your comment.

Create extension