Advertisement
slovacus

Plugin Error Handler

Jun 13th, 2012
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.01 KB | None | 0 0
  1. <?php
  2.  
  3. class Core_controller_Plugin_ErrorHandler extends Zend_Controller_Plugin_ErrorHandler
  4. {
  5.  
  6.     const EXCEPTION_ACL = 'EXCEPTION_ACL';
  7.  
  8.     protected function _handleError(Zend_Controller_Request_Abstract $request)
  9.     {
  10.         $frontController = Zend_Controller_Front::getInstance();
  11.         if ($frontController->getParam('noErrorHandler')) {
  12.             return;
  13.         }
  14.  
  15.         $response = $this->getResponse();
  16.  
  17.         if ($this->_isInsideErrorHandlerLoop) {
  18.             $exceptions = $response->getException();
  19.             if (count($exceptions) > $this->_exceptionCountAtFirstEncounter) {
  20.                 // Exception thrown by error handler; tell the front controller to throw it
  21.                 $frontController->throwExceptions(true);
  22.                 throw array_pop($exceptions);
  23.             }
  24.         }
  25.  
  26.         // check for an exception AND allow the error handler controller the option to forward
  27.         if (($response->isException()) && (!$this->_isInsideErrorHandlerLoop)) {
  28.             $this->_isInsideErrorHandlerLoop = true;
  29.  
  30.             // Get exception information
  31.             $error = new ArrayObject(array(), ArrayObject::ARRAY_AS_PROPS);
  32.             $exceptions = $response->getException();
  33.             $exception = $exceptions[0];
  34.             $exceptionType = get_class($exception);
  35.             $error->exception = $exception;
  36.             switch ($exceptionType) {
  37.                 case 'Zend_Controller_Router_Exception':
  38.                     if (404 == $exception->getCode()) {
  39.                         $error->type = self::EXCEPTION_NO_ROUTE;
  40.                     } else {
  41.                         $error->type = self::EXCEPTION_OTHER;
  42.                     }
  43.                     break;
  44.                 case 'Zend_Controller_Dispatcher_Exception':
  45.                     $error->type = self::EXCEPTION_NO_CONTROLLER;
  46.                     break;
  47.                 case 'Zend_Controller_Action_Exception':
  48.                     if (404 == $exception->getCode()) {
  49.                         $error->type = self::EXCEPTION_NO_ACTION;
  50.                     } else {
  51.                         $error->type = self::EXCEPTION_OTHER;
  52.                     }
  53.                     break;
  54.                 case 'Zend_Acl_Exception':
  55.                     $error->type = self::EXCEPTION_ACL;
  56.                 default:
  57.                     $error->type = self::EXCEPTION_OTHER;
  58.                     break;
  59.             }
  60.  
  61.             // Keep a copy of the original request
  62.             $error->request = clone $request;
  63.  
  64.             // get a count of the number of exceptions encountered
  65.             $this->_exceptionCountAtFirstEncounter = count($exceptions);
  66.  
  67.             // Forward to the error handler
  68.             $request->setParam('error_handler', $error)
  69.                 ->setModuleName($this->getErrorHandlerModule())
  70.                 ->setControllerName($this->getErrorHandlerController())
  71.                 ->setActionName($this->getErrorHandlerAction())
  72.                 ->setDispatched(false);
  73.         }
  74.     }
  75.  
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement