Advertisement
dragonbe

ErrorHandler plugin

Oct 10th, 2012
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.16 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Handle exceptions that bubble up based on missing controllers, actions,
  4.  * application errors, or user defined errors and forward to an error handler.
  5.  *
  6.  * To handle custom error:
  7.  *  1. Define exception constant
  8.  *  2. Define Error Handler Action for custom exception
  9.  *  <code>
  10.  *    case 'Core_Exception_CustomOne':
  11.  *         $error->type = self::EXCEPTION_CUSTOM_ONE;
  12.  *         $this->setErrorHandlerAction('customone');
  13.  *         break;
  14.  *  </code>
  15.  *
  16.  * @uses Zend_Controller_Plugin_ErrorHandler
  17.  * @package zfCustomError
  18.  * @subpackage Plugins
  19.  */
  20. class Core_Controller_Plugin_ErrorHandler extends Zend_Controller_Plugin_ErrorHandler
  21. {
  22.  
  23.     /**
  24.      * Const - Item not found exception; item does not exist
  25.      */
  26.     const EXCEPTION_ITEM_NOT_FOUND = 'EXCEPTION_ITEM_NOT_FOUND';
  27.  
  28.     /**
  29.      * Const - No access exception; Actor has no access to the resource
  30.      */
  31.     const EXCEPTION_NO_ACCESS = 'EXCEPTION_NO_ACCESS';
  32.  
  33.     /**
  34.      * Const - Maintenance mode exception; Site section in maintenance mode
  35.      */
  36.     const EXCEPTION_MAINTENANCE_MODE = 'EXCEPTION_MAINTENANCE_MODE';
  37.    
  38.     // @todo add more EXCEPTION constant if needed to define exception type
  39.     // const EXCEPTION_NAME = 'EXCEPTION_NAME';
  40.    
  41.     /**
  42.      * Handle errors and exceptions
  43.      *
  44.      * If the 'noErrorHandler' front controller flag has been set,
  45.      * returns early.
  46.      *
  47.      * @param $request Zend_Controller_Request_Abstract          
  48.      * @return void
  49.      */
  50.     protected function _handleError (Zend_Controller_Request_Abstract $request)
  51.     {
  52.         $frontController = Zend_Controller_Front::getInstance();
  53.         $response = $this->getResponse();
  54.        
  55.         if ($this->_isInsideErrorHandlerLoop) {
  56.             $exceptions = $response->getException();
  57.             if (count($exceptions) > $this->_exceptionCountAtFirstEncounter) {
  58.                 // Exception thrown by error handler; tell the front controller
  59.                 // to throw it
  60.                 $frontController->throwExceptions(true);
  61.                 throw array_pop($exceptions);
  62.             }
  63.         }
  64.        
  65.         // check for an exception AND allow the error handler controller the
  66.         // option to forward
  67.         if (($response->isException()) && (! $this->_isInsideErrorHandlerLoop)) {
  68.             $this->_isInsideErrorHandlerLoop = true;
  69.            
  70.             // Get exception information
  71.             $error = new ArrayObject(array(), ArrayObject::ARRAY_AS_PROPS);
  72.             $exceptions = $response->getException();
  73.             $exception = $exceptions[0];
  74.             $exceptionType = get_class($exception);
  75.             $error->exception = $exception;
  76.             switch ($exceptionType) {
  77.                 case 'Zend_Controller_Router_Exception':
  78.                     if (404 == $exception->getCode()) {
  79.                         $error->type = self::EXCEPTION_NO_ROUTE;
  80.                     } else {
  81.                         $error->type = self::EXCEPTION_OTHER;
  82.                     }
  83.                     break;
  84.                 case 'Zend_Controller_Dispatcher_Exception':
  85.                     $error->type = self::EXCEPTION_NO_CONTROLLER;
  86.                     break;
  87.                 case 'Zend_Controller_Action_Exception':
  88.                     if (404 == $exception->getCode()) {
  89.                         $error->type = self::EXCEPTION_NO_ACTION;
  90.                     } else {
  91.                         $error->type = self::EXCEPTION_OTHER;
  92.                     }
  93.                     break;
  94.                 case 'Core_Exception_ItemNotFound':
  95.                     $error->type = self::EXCEPTION_ITEM_NOT_FOUND;
  96.                     $this->setErrorHandlerAction('item');
  97.                     break;
  98.                 case 'Core_Exception_NoAccess':
  99.                     $error->type = self::EXCEPTION_NO_ACCESS;
  100.                     $this->setErrorHandlerAction('noaccess');
  101.                     break;
  102.                 case 'Core_Exception_MaintenanceMode':
  103.                     $error->type = self::EXCEPTION_MAINTENANCE_MODE;
  104.                     $this->setErrorHandlerAction('maintenance');
  105.                     break;
  106.                 // @todo add more cases for custom exceptions
  107.                 // case 'Core_Exception_ExceptionName' :
  108.                 // $error->type = self::EXCEPTION_NAME;
  109.                 // $this->setErrorHandlerAction('ExceptionName');
  110.                 // break;
  111.                 default:
  112.                     $error->type = self::EXCEPTION_OTHER;
  113.                     break;
  114.             }
  115.            
  116.             // Keep a copy of the original request
  117.             $error->request = clone $request;
  118.            
  119.             // get a count of the number of exceptions encountered
  120.             $this->_exceptionCountAtFirstEncounter = count($exceptions);
  121.            
  122.             // Forward to the error handler
  123.             $request->setParam('error_handler', $error)
  124.                 ->setModuleName($this->getErrorHandlerModule())
  125.                 ->setControllerName($this->getErrorHandlerController())
  126.                 ->setActionName($this->getErrorHandlerAction())
  127.                 ->setDispatched(false);
  128.         }
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement