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

Allow handling any Throwable #7

Merged
merged 1 commit into from
May 31, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}