Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit d374f7c

Browse files
committed
Merge branch 'hotfix/138'
Close #138
2 parents 39596e7 + 51c0f73 commit d374f7c

9 files changed

+74
-13
lines changed

CHANGELOG.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22

33
All notable changes to this project will be documented in this file, in reverse chronological order by release.
44

5-
## 2.7.8 - TBD
5+
## 2.7.8 - 2016-05-31
66

77
### Added
88

9-
- Nothing.
9+
- [#138](https://github.com/zendframework/zend-mvc/pull/138) adds support for
10+
PHP 7 `Throwable`s within each of:
11+
- `DispatchListener`
12+
- `MiddlewareListener`
13+
- The console `RouteNotFoundStrategy` and `ExceptionStrategy`
14+
- The HTTP `DefaultRenderingStrategy` and `RouteNotFoundStrategy`
1015

1116
### Deprecated
1217

src/DispatchListener.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,10 @@ public function onDispatch(MvcEvent $e)
9898
} catch (InvalidServiceException $exception) {
9999
$return = $this->marshalControllerNotFoundEvent($application::ERROR_CONTROLLER_INVALID, $controllerName, $e, $application, $exception);
100100
return $this->complete($return, $e);
101-
} catch (\Exception $exception) {
101+
} catch (\Throwable $exception) {
102+
$return = $this->marshalBadControllerEvent($controllerName, $e, $application, $exception);
103+
return $this->complete($return, $e);
104+
} catch (\Exception $exception) { // @TODO clean up once PHP 7 requirement is enforced
102105
$return = $this->marshalBadControllerEvent($controllerName, $e, $application, $exception);
103106
return $this->complete($return, $e);
104107
}
@@ -109,15 +112,22 @@ public function onDispatch(MvcEvent $e)
109112

110113
$request = $e->getRequest();
111114
$response = $application->getResponse();
115+
$caughtException = null;
112116

113117
try {
114118
$return = $controller->dispatch($request, $response);
115-
} catch (\Exception $ex) {
119+
} catch (\Throwable $ex) {
120+
$caughtException = $ex;
121+
} catch (\Exception $ex) { // @TODO clean up once PHP 7 requirement is enforced
122+
$caughtException = $ex;
123+
}
124+
125+
if ($caughtException !== null) {
116126
$e->setName(MvcEvent::EVENT_DISPATCH_ERROR);
117127
$e->setError($application::ERROR_EXCEPTION);
118128
$e->setController($controllerName);
119129
$e->setControllerClass(get_class($controller));
120-
$e->setParam('exception', $ex);
130+
$e->setParam('exception', $caughtException);
121131

122132
$return = $application->getEventManager()->triggerEvent($e)->last();
123133
if (! $return) {
@@ -135,7 +145,7 @@ public function reportMonitorEvent(MvcEvent $e)
135145
{
136146
$error = $e->getError();
137147
$exception = $e->getParam('exception');
138-
if ($exception instanceof \Exception) {
148+
if ($exception instanceof \Exception || $exception instanceof \Throwable) { // @TODO clean up once PHP 7 requirement is enforced
139149
zend_monitor_custom_event_ex($error, $exception->getMessage(), 'Zend Framework Exception', ['code' => $exception->getCode(), 'trace' => $exception->getTraceAsString()]);
140150
}
141151
}

src/MiddlewareListener.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,17 @@ public function onDispatch(MvcEvent $event)
5656
$event->setResult($return);
5757
return $return;
5858
}
59+
60+
$caughtException = null;
5961
try {
6062
$return = $middleware(Psr7Request::fromZend($request), Psr7Response::fromZend($response));
61-
} catch (\Exception $exception) {
63+
} catch (\Throwable $exception) {
64+
$caughtException = $exception;
65+
} catch (\Exception $exception) { // @TODO clean up once PHP 7 requirement is enforced
66+
$caughtException = $exception;
67+
}
68+
69+
if ($caughtException !== null) {
6270
$event->setName(MvcEvent::EVENT_DISPATCH_ERROR);
6371
$event->setError($application::ERROR_EXCEPTION);
6472
$event->setController($middlewareName);

src/View/Console/ExceptionStrategy.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,10 @@ public function prepareExceptionViewModel(MvcEvent $e)
184184
if (is_callable($this->message)) {
185185
$callback = $this->message;
186186
$message = (string) $callback($exception, $this->displayExceptions);
187-
} elseif ($this->displayExceptions && $exception instanceof \Exception) {
187+
} elseif ($this->displayExceptions
188+
// @TODO clean up once PHP 7 requirement is enforced
189+
&& ($exception instanceof \Exception || $exception instanceof \Throwable)
190+
) {
188191
$previous = '';
189192
$previousException = $exception->getPrevious();
190193
while ($previousException) {

src/View/Console/RouteNotFoundStrategy.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,8 @@ protected function reportNotFoundReason($e)
460460
];
461461
$report = sprintf("\nReason for failure: %s\n", $reasons[$reason]);
462462

463-
while ($exception instanceof \Exception) {
463+
// @TODO clean up once PHP 7 requirement is enforced
464+
while ($exception instanceof \Exception || $exception instanceof \Throwable) {
464465
$report .= sprintf("Exception: %s\nTrace:\n%s\n", $exception->getMessage(), $exception->getTraceAsString());
465466
$exception = $exception->getPrevious();
466467
}

src/View/Http/DefaultRenderingStrategy.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,17 @@ public function render(MvcEvent $e)
9999
$view->setRequest($request);
100100
$view->setResponse($response);
101101

102+
$caughtException = null;
103+
102104
try {
103105
$view->render($viewModel);
104-
} catch (\Exception $ex) {
106+
} catch (\Throwable $ex) {
107+
$caughtException = $ex;
108+
} catch (\Exception $ex) { // @TODO clean up once PHP 7 requirement is enforced
109+
$caughtException = $ex;
110+
}
111+
112+
if ($caughtException !== null) {
105113
if ($e->getName() === MvcEvent::EVENT_RENDER_ERROR) {
106114
throw $ex;
107115
}

src/View/Http/RouteNotFoundStrategy.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,9 @@ protected function injectException($model, $e)
250250
$model->setVariable('display_exceptions', true);
251251

252252
$exception = $e->getParam('exception', false);
253-
if (!$exception instanceof \Exception) {
253+
254+
// @TODO clean up once PHP 7 requirement is enforced
255+
if (!$exception instanceof \Exception && !$exception instanceof \Throwable) {
254256
return;
255257
}
256258

test/ApplicationTest.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ public function setupActionController()
291291
return $this->application;
292292
}
293293

294-
public function setupBadController($addService = true)
294+
public function setupBadController($addService = true, $action = 'test')
295295
{
296296
$request = $this->serviceManager->get('Request');
297297
$request->setUri('http://example.local/bad');
@@ -301,7 +301,7 @@ public function setupBadController($addService = true)
301301
'route' => '/bad',
302302
'defaults' => [
303303
'controller' => 'bad',
304-
'action' => 'test',
304+
'action' => $action,
305305
],
306306
]);
307307
$router->addRoute('bad', $route);
@@ -374,6 +374,25 @@ public function testLocatorExceptionShouldTriggerDispatchError()
374374
$this->assertSame($response, $result->getResponse(), get_class($result));
375375
}
376376

377+
/**
378+
* @requires PHP 7.0
379+
* @group error-handling
380+
*/
381+
public function testPhp7ErrorRaisedInDispatchableShouldRaiseDispatchErrorEvent()
382+
{
383+
$this->setupBadController(true, 'test-php7-error');
384+
$response = $this->application->getResponse();
385+
$events = $this->application->getEventManager();
386+
$events->attach(MvcEvent::EVENT_DISPATCH_ERROR, function ($e) use ($response) {
387+
$exception = $e->getParam('exception');
388+
$response->setContent($exception->getMessage());
389+
return $response;
390+
});
391+
392+
$this->application->run();
393+
$this->assertContains('Raised an error', $response->getContent());
394+
}
395+
377396
/**
378397
* @group error-handling
379398
*/

test/Controller/TestAsset/BadController.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,9 @@ public function testAction()
1717
{
1818
throw new \Exception('Raised an exception');
1919
}
20+
21+
public function testPhp7ErrorAction()
22+
{
23+
throw new \Error('Raised an error');
24+
}
2025
}

0 commit comments

Comments
 (0)