Advertisement
dragonbe

Custom Error Handler actions

Oct 10th, 2012
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.82 KB | None | 0 0
  1. // ErrorController.php
  2.  
  3. <?php
  4. class ErrorController extends Zend_Controller_Action
  5. {
  6.  
  7.     /**
  8.      *
  9.      * @see Zend_Controller_Action::init()
  10.      */
  11.     public function init ()
  12.     {
  13.         $this->_errors = $this->_getParam('error_handler');
  14.     }
  15.  
  16.     /**
  17.      * Pass to view exception and request details
  18.      *
  19.      * @see Zend_Controller_Action::preDispatch()
  20.      */
  21.     public function preDispatch ()
  22.     {
  23.         // Log exception, if logger available
  24.         if ($log = $this->getLog()) {
  25.             $log->crit($this->view->message, $this->_errors->exception);
  26.         }
  27.         // conditionally display exceptions
  28.         if ($this->getInvokeArg('displayExceptions') == TRUE) {
  29.             $this->view->exception = $this->_errors->exception;
  30.         }
  31.         $this->view->request = $this->_errors->request;
  32.     }
  33.  
  34.     /**
  35.      * Default Error handler action
  36.      */
  37.     public function errorAction ()
  38.     {
  39.         switch ($this->_errors->type) {
  40.             case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
  41.             case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
  42.             case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
  43.                 // 404 error -- controller or action not found
  44.                 $this->getResponse()->setHttpResponseCode(404);
  45.                 $this->view->message = 'Page not found';
  46.                 break;
  47.             default:
  48.                 // application error
  49.                 $this->getResponse()->setHttpResponseCode(500);
  50.                 $this->view->message = 'Application error';
  51.                 break;
  52.         }
  53.     }
  54.  
  55.     /**
  56.      * Custom Error handler action for Core_Exception_ItemNotFound exception
  57.      */
  58.     public function itemAction ()
  59.     {
  60.         $this->getResponse()->setHttpResponseCode(404);
  61.         $this->view->message = $this->_errors->exception->getMessage();
  62.     }
  63.  
  64.     /**
  65.      * Custom Error handler action for Core_Exception_NoAccess exception
  66.      */
  67.     public function noaccessAction ()
  68.     {
  69.         $this->getResponse()->setHttpResponseCode(401);
  70.         $this->view->message = $this->_errors->exception->getMessage();
  71.     }
  72.  
  73.     /**
  74.      * Custom Error handler action for Core_Exception_MaintenanceMode exception
  75.      */
  76.     public function maintenanceAction ()
  77.     {
  78.         $this->view->message = $this->_errors->exception->getMessage();
  79.     }
  80.  
  81.     /**
  82.      *
  83.      * @return Ambigous <NULL, mixed>|boolean
  84.      */
  85.     public function getLog ()
  86.     {
  87.         $bootstrap = $this->getInvokeArg('bootstrap');
  88.         if ($bootstrap instanceof Zend_Application_Bootstrap_Bootstrap &&
  89.                  $bootstrap->hasPluginResource('Log')) {
  90.                     return $bootstrap->getResource('Log');
  91.                 }
  92.                 return FALSE;
  93.             }
  94.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement