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

Commit

Permalink
Merge branch 'hotfix/138' into develop
Browse files Browse the repository at this point in the history
Forward port #138
  • Loading branch information
weierophinney committed May 31, 2016
2 parents ff9d145 + 51c0f73 commit 588f18a
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 11 deletions.
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,16 @@ for full details on how to migrate your v2 application.
- [#128](https://github.com/zendframework/zend-mvc/pull/128) bumps the minimum
supported PHP version to 5.6.

## 2.7.8 - TBD
## 2.7.8 - 2016-05-31

### Added

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

### Deprecated

Expand Down
18 changes: 14 additions & 4 deletions src/DispatchListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ public function onDispatch(MvcEvent $e)
} catch (InvalidServiceException $exception) {
$return = $this->marshalControllerNotFoundEvent($application::ERROR_CONTROLLER_INVALID, $controllerName, $e, $application, $exception);
return $this->complete($return, $e);
} catch (\Exception $exception) {
} catch (\Throwable $exception) {
$return = $this->marshalBadControllerEvent($controllerName, $e, $application, $exception);
return $this->complete($return, $e);
} catch (\Exception $exception) { // @TODO clean up once PHP 7 requirement is enforced
$return = $this->marshalBadControllerEvent($controllerName, $e, $application, $exception);
return $this->complete($return, $e);
}
Expand All @@ -110,15 +113,22 @@ public function onDispatch(MvcEvent $e)

$request = $e->getRequest();
$response = $application->getResponse();
$caughtException = null;

try {
$return = $controller->dispatch($request, $response);
} catch (\Exception $ex) {
} catch (\Throwable $ex) {
$caughtException = $ex;
} catch (\Exception $ex) { // @TODO clean up once PHP 7 requirement is enforced
$caughtException = $ex;
}

if ($caughtException !== null) {
$e->setName(MvcEvent::EVENT_DISPATCH_ERROR);
$e->setError($application::ERROR_EXCEPTION);
$e->setController($controllerName);
$e->setControllerClass(get_class($controller));
$e->setParam('exception', $ex);
$e->setParam('exception', $caughtException);

$return = $application->getEventManager()->triggerEvent($e)->last();
if (! $return) {
Expand All @@ -136,7 +146,7 @@ public function reportMonitorEvent(MvcEvent $e)
{
$error = $e->getError();
$exception = $e->getParam('exception');
if ($exception instanceof \Exception) {
if ($exception instanceof \Exception || $exception instanceof \Throwable) { // @TODO clean up once PHP 7 requirement is enforced
zend_monitor_custom_event_ex($error, $exception->getMessage(), 'Zend Framework Exception', ['code' => $exception->getCode(), 'trace' => $exception->getTraceAsString()]);
}
}
Expand Down
10 changes: 9 additions & 1 deletion src/MiddlewareListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,17 @@ public function onDispatch(MvcEvent $event)
$event->setResult($return);
return $return;
}

$caughtException = null;
try {
$return = $middleware(Psr7Request::fromZend($request), Psr7Response::fromZend($response));
} catch (\Exception $exception) {
} catch (\Throwable $exception) {
$caughtException = $exception;
} catch (\Exception $exception) { // @TODO clean up once PHP 7 requirement is enforced
$caughtException = $exception;
}

if ($caughtException !== null) {
$event->setName(MvcEvent::EVENT_DISPATCH_ERROR);
$event->setError($application::ERROR_EXCEPTION);
$event->setController($middlewareName);
Expand Down
10 changes: 9 additions & 1 deletion src/View/Http/DefaultRenderingStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,17 @@ public function render(MvcEvent $e)
$view->setRequest($request);
$view->setResponse($response);

$caughtException = null;

try {
$view->render($viewModel);
} catch (\Exception $ex) {
} catch (\Throwable $ex) {
$caughtException = $ex;
} catch (\Exception $ex) { // @TODO clean up once PHP 7 requirement is enforced
$caughtException = $ex;
}

if ($caughtException !== null) {
if ($e->getName() === MvcEvent::EVENT_RENDER_ERROR) {
throw $ex;
}
Expand Down
4 changes: 3 additions & 1 deletion src/View/Http/RouteNotFoundStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,9 @@ protected function injectException($model, $e)
$model->setVariable('display_exceptions', true);

$exception = $e->getParam('exception', false);
if (!$exception instanceof \Exception) {

// @TODO clean up once PHP 7 requirement is enforced
if (!$exception instanceof \Exception && !$exception instanceof \Throwable) {
return;
}

Expand Down
23 changes: 21 additions & 2 deletions test/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ public function setupActionController()
return $this->application;
}

public function setupBadController($addService = true)
public function setupBadController($addService = true, $action = 'test')
{
$request = $this->serviceManager->get('Request');
$request->setUri('http://example.local/bad');
Expand All @@ -308,7 +308,7 @@ public function setupBadController($addService = true)
'route' => '/bad',
'defaults' => [
'controller' => 'bad',
'action' => 'test',
'action' => $action,
],
]);
$router->addRoute('bad', $route);
Expand Down Expand Up @@ -381,6 +381,25 @@ public function testLocatorExceptionShouldTriggerDispatchError()
$this->assertSame($response, $result->getResponse(), get_class($result));
}

/**
* @requires PHP 7.0
* @group error-handling
*/
public function testPhp7ErrorRaisedInDispatchableShouldRaiseDispatchErrorEvent()
{
$this->setupBadController(true, 'test-php7-error');
$response = $this->application->getResponse();
$events = $this->application->getEventManager();
$events->attach(MvcEvent::EVENT_DISPATCH_ERROR, function ($e) use ($response) {
$exception = $e->getParam('exception');
$response->setContent($exception->getMessage());
return $response;
});

$this->application->run();
$this->assertContains('Raised an error', $response->getContent());
}

/**
* @group error-handling
*/
Expand Down
5 changes: 5 additions & 0 deletions test/Controller/TestAsset/BadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@ public function testAction()
{
throw new \Exception('Raised an exception');
}

public function testPhp7ErrorAction()
{
throw new \Error('Raised an error');
}
}

0 comments on commit 588f18a

Please sign in to comment.