PcCustomErrorActionFilter ¶
- Features
- Usage
- Resources
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() {
$error = Yii::app()->errorHandler->error;
unset($error['traces']);
Yii::log("Fatal Error occurred! This was the reported error:\n" .
var_export($error, true), CLogger::LEVEL_WARNING, __METHOD__);
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(
'application.components.PcCustomErrorActionFilter - handleErrors',
'errorActionRoute' => 'moduleId/controllerId/handleErrors',
),
);
}
Resources ¶
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.