3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Mvc\View\Http
;
12 use Zend\EventManager\AbstractListenerAggregate
;
13 use Zend\EventManager\EventManagerInterface
;
14 use Zend\Http\Response
as HttpResponse
;
15 use Zend\Mvc\Application
;
16 use Zend\Mvc\MvcEvent
;
17 use Zend\Stdlib\ResponseInterface
as Response
;
18 use Zend\View\Model\ViewModel
;
20 class ExceptionStrategy
extends AbstractListenerAggregate
26 protected $displayExceptions = false;
29 * Name of exception template
32 protected $exceptionTemplate = 'error';
37 public function attach(EventManagerInterface
$events)
39 $this->listeners
[] = $events->attach(MvcEvent
::EVENT_DISPATCH_ERROR
, array($this, 'prepareExceptionViewModel'));
40 $this->listeners
[] = $events->attach(MvcEvent
::EVENT_RENDER_ERROR
, array($this, 'prepareExceptionViewModel'));
44 * Flag: display exceptions in error pages?
46 * @param bool $displayExceptions
47 * @return ExceptionStrategy
49 public function setDisplayExceptions($displayExceptions)
51 $this->displayExceptions
= (bool) $displayExceptions;
56 * Should we display exceptions in error pages?
60 public function displayExceptions()
62 return $this->displayExceptions
;
66 * Set the exception template
68 * @param string $exceptionTemplate
69 * @return ExceptionStrategy
71 public function setExceptionTemplate($exceptionTemplate)
73 $this->exceptionTemplate
= (string) $exceptionTemplate;
78 * Retrieve the exception template
82 public function getExceptionTemplate()
84 return $this->exceptionTemplate
;
88 * Create an exception view model, and set the HTTP status code
90 * @todo dispatch.error does not halt dispatch unless a response is
91 * returned. As such, we likely need to trigger rendering as a low
92 * priority dispatch.error event (or goto a render event) to ensure
93 * rendering occurs, and that munging of view models occurs when
98 public function prepareExceptionViewModel(MvcEvent
$e)
100 // Do nothing if no error in the event
101 $error = $e->getError();
106 // Do nothing if the result is a response object
107 $result = $e->getResult();
108 if ($result instanceof Response
) {
113 case Application
::ERROR_CONTROLLER_NOT_FOUND
:
114 case Application
::ERROR_CONTROLLER_INVALID
:
115 case Application
::ERROR_ROUTER_NO_MATCH
:
116 // Specifically not handling these
119 case Application
::ERROR_EXCEPTION
:
121 $model = new ViewModel(array(
122 'message' => 'An error occurred during execution; please try again later.',
123 'exception' => $e->getParam('exception'),
124 'display_exceptions' => $this->displayExceptions(),
126 $model->setTemplate($this->getExceptionTemplate());
127 $e->setResult($model);
129 $response = $e->getResponse();
131 $response = new HttpResponse();
132 $response->setStatusCode(500);
133 $e->setResponse($response);
135 $statusCode = $response->getStatusCode();
136 if ($statusCode === 200) {
137 $response->setStatusCode(500);