Skip to content

Commit

Permalink
Allow handling any Throwable
Browse files Browse the repository at this point in the history
Based on zendframework/zend-mvc#138, this patch adds support for
handling any Throwable, not just Exceptions, when reporting exceptions
and route not found information; the patch includes tests of both
changes in behavior.
  • Loading branch information
weierophinney committed May 31, 2016
1 parent be51bb7 commit 9d8042d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/View/ExceptionStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,10 @@ public function prepareExceptionViewModel(MvcEvent $e)
if (is_callable($this->message)) {
$callback = $this->message;
$message = (string) $callback($exception, $this->displayExceptions);
} elseif ($this->displayExceptions && $exception instanceof \Exception) {
} elseif ($this->displayExceptions
// @todo clean up once PHP 7 requirement is enforced
&& ($exception instanceof \Exception || $exception instanceof \Throwable)
) {
$previous = '';
$previousException = $exception->getPrevious();
while ($previousException) {
Expand Down
3 changes: 2 additions & 1 deletion src/View/RouteNotFoundStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,8 @@ protected function reportNotFoundReason($e)
];
$report = sprintf("\nReason for failure: %s\n", $reasons[$reason]);

while ($exception instanceof \Exception) {
// @todo clean up once PHP 7 requirement is enforced
while ($exception instanceof \Exception || $exception instanceof \Throwable) {
$report .= sprintf(
"Exception: %s\nTrace:\n%s\n",
$exception->getMessage(),
Expand Down
18 changes: 16 additions & 2 deletions test/View/ExceptionStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,29 @@ public function testPrepareExceptionViewModelErrorsThatMustGetSameResult()
}
}

public function testPrepareExceptionViewModelErrorException()
public function throwables()
{
$throwables = ['exception' => [\Exception::class]];

if (version_compare(PHP_VERSION, '7.0.0', '>=')) {
$throwables['error'] = [\Error::class];
}

return $throwables;
}

/**
* @dataProvider throwables
*/
public function testPrepareExceptionViewModelErrorException($throwable)
{
$errors = [Application::ERROR_EXCEPTION, 'user-defined-error'];

foreach ($errors as $error) {
$events = new EventManager();
$this->strategy->attach($events);

$exception = new \Exception('message foo');
$exception = new $throwable('message foo');
$event = new MvcEvent(MvcEvent::EVENT_DISPATCH_ERROR, null, ['exception' => $exception]);

$event->setError($error);
Expand Down
28 changes: 28 additions & 0 deletions test/View/RouteNotFoundStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PHPUnit_Framework_TestCase as TestCase;
use Prophecy\Argument;
use ReflectionClass;
use ReflectionMethod;
use Zend\Console\Adapter\AdapterInterface;
use Zend\Console\ColorInterface;
use Zend\Console\Request;
Expand Down Expand Up @@ -233,4 +234,31 @@ public function testSetsResultToPopulatedViewModelWhenSuccessful($type, $reasonM

$this->assertNull($this->strategy->handleRouteNotFoundError($event->reveal()));
}

public function throwables()
{
$throwables = ['exception' => [\Exception::class]];

if (version_compare(PHP_VERSION, '7.0.0', '>=')) {
$throwables['error'] = [\Error::class];
}

return $throwables;
}

/**
* @dataProvider throwables
*/
public function testWillTraceAnyThrowableWhenAllowedToReportNotFoundReason($throwable)
{
$event = new MvcEvent();
$event->setParam('exception', new $throwable('EXCEPTION THROWN'));

$r = new ReflectionMethod($this->strategy, 'reportNotFoundReason');
$r->setAccessible(true);

$report = $r->invoke($this->strategy, $event);
$this->assertContains('Reason for failure: Unknown', $report);
$this->assertContains('Exception: EXCEPTION THROWN', $report);
}
}

0 comments on commit 9d8042d

Please sign in to comment.