Skip to content

Commit

Permalink
Merge pull request #22 from GrahamCampbell/access
Browse files Browse the repository at this point in the history
Allowed access to exceptions before transformation
  • Loading branch information
GrahamCampbell committed Jul 25, 2015
2 parents 5266b1b + 5043547 commit 001880b
Show file tree
Hide file tree
Showing 16 changed files with 80 additions and 51 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ CHANGE LOG
* Resolve all the config earlier
* Made the html displayer responsive
* Added exception levels
* Allowed access to exceptions before transformation


## V3.2 (06/07/2015)
Expand Down
5 changes: 3 additions & 2 deletions src/Displayers/DebugDisplayer.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,12 @@ public function contentType()
/**
* Can we display the exception?
*
* @param \Exception $exception
* @param \Exception $original
* @param \Exception $transformed
*
* @return bool
*/
public function canDisplay(Exception $exception)
public function canDisplay(Exception $original, Exception $transformed)
{
return true;
}
Expand Down
5 changes: 3 additions & 2 deletions src/Displayers/DisplayerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ public function contentType();
/**
* Can we display the exception?
*
* @param \Exception $exception
* @param \Exception $original
* @param \Exception $transformed
*
* @return bool
*/
public function canDisplay(Exception $exception);
public function canDisplay(Exception $original, Exception $transformed);

/**
* Do we provide verbose information about the exception?
Expand Down
5 changes: 3 additions & 2 deletions src/Displayers/HtmlDisplayer.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,12 @@ public function contentType()
/**
* Can we display the exception?
*
* @param \Exception $exception
* @param \Exception $original
* @param \Exception $transformed
*
* @return bool
*/
public function canDisplay(Exception $exception)
public function canDisplay(Exception $original, Exception $transformed)
{
return true;
}
Expand Down
5 changes: 3 additions & 2 deletions src/Displayers/JsonDisplayer.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,12 @@ public function contentType()
/**
* Can we display the exception?
*
* @param \Exception $exception
* @param \Exception $original
* @param \Exception $transformed
*
* @return bool
*/
public function canDisplay(Exception $exception)
public function canDisplay(Exception $original, Exception $transformed)
{
return true;
}
Expand Down
22 changes: 12 additions & 10 deletions src/ExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,14 @@ protected function getLevel(Exception $exception)
public function render($request, Exception $e)
{
$id = $this->container->make(ExceptionIdentifier::class)->identify($e);
$e = $this->getTransformed($e);
$flattened = FlattenException::create($e);
$transformed = $this->getTransformed($e);
$flattened = FlattenException::create($transformed);
$code = $flattened->getStatusCode();
$headers = $flattened->getHeaders();

$response = $this->getDisplayer($e)->display($e, $id, $code, $headers);
$response = $this->getDisplayer($e, $transformed)->display($transformed, $id, $code, $headers);

return $this->toIlluminateResponse($response, $e);
return $this->toIlluminateResponse($response, $transformed);
}

/**
Expand All @@ -138,15 +138,16 @@ protected function getTransformed(Exception $exception)
/**
* Get the displayer instance.
*
* @param \Exception $exception
* @param \Exception $original
* @param \Exception $transformed
*
* @return \GrahamCampbell\Exceptions\Displayers\DisplayerInterface
*/
protected function getDisplayer(Exception $exception)
protected function getDisplayer(Exception $original, Exception $transformed)
{
$displayers = $this->make(array_get($this->config, 'displayers', []));

if ($filtered = $this->getFiltered($displayers, $exception)) {
if ($filtered = $this->getFiltered($displayers, $original, $transformed)) {
return $filtered[0];
}

Expand All @@ -157,14 +158,15 @@ protected function getDisplayer(Exception $exception)
* Get the filtered list of displayers.
*
* @param \GrahamCampbell\Exceptions\Displayers\DisplayerInterface[] $displayers
* @param \Exception $exception
* @param \Exception $original
* @param \Exception $transformed
*
* @return \GrahamCampbell\Exceptions\Displayers\DisplayerInterface[]
*/
protected function getFiltered(array $displayers, Exception $exception)
protected function getFiltered(array $displayers, Exception $original, Exception $transformed)
{
foreach ($this->make(array_get($this->config, 'filters', [])) as $filter) {
$displayers = $filter->filter($displayers, $exception);
$displayers = $filter->filter($displayers, $original, $transformed);
}

return array_values($displayers);
Expand Down
7 changes: 4 additions & 3 deletions src/Filters/CanDisplayFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ class CanDisplayFilter
* Filter and return the displayers.
*
* @param \GrahamCampbell\Exceptions\Displayers\DisplayerInterface[] $displayers
* @param \Exception $exception
* @param \Exception $original
* @param \Exception $transformed
*
* @return \GrahamCampbell\Exceptions\Displayers\DisplayerInterface[]
*/
public function filter(array $displayers, Exception $exception)
public function filter(array $displayers, Exception $original, Exception $transformed)
{
foreach ($displayers as $index => $displayer) {
if (!$displayer->canDisplay($exception)) {
if (!$displayer->canDisplay($original, $transformed)) {
unset($displayers[$index]);
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/Filters/ContentTypeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ public function __construct(Request $request)
* Filter and return the displayers.
*
* @param \GrahamCampbell\Exceptions\Displayers\DisplayerInterface[] $displayers
* @param \Exception $exception
* @param \Exception $original
* @param \Exception $transformed
*
* @return \GrahamCampbell\Exceptions\Displayers\DisplayerInterface[]
*/
public function filter(array $displayers, Exception $exception)
public function filter(array $displayers, Exception $original, Exception $transformed)
{
foreach ($displayers as $index => $displayer) {
if (!$this->request->accepts($displayer->contentType())) {
Expand Down
5 changes: 3 additions & 2 deletions src/Filters/FilterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ interface FilterInterface
* Filter and return the displayers.
*
* @param \GrahamCampbell\Exceptions\Displayers\DisplayerInterface[] $displayers
* @param \Exception $exception
* @param \Exception $original
* @param \Exception $transformed
*
* @return \GrahamCampbell\Exceptions\Displayers\DisplayerInterface[]
*/
public function filter(array $displayers, Exception $exception);
public function filter(array $displayers, Exception $original, Exception $transformed);
}
5 changes: 3 additions & 2 deletions src/Filters/VerboseFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ public function __construct(Config $config)
* Filter and return the displayers.
*
* @param \GrahamCampbell\Exceptions\Displayers\DisplayerInterface[] $displayers
* @param \Exception $exception
* @param \Exception $original
* @param \Exception $transformed
*
* @return \GrahamCampbell\Exceptions\Displayers\DisplayerInterface[]
*/
public function filter(array $displayers, Exception $exception)
public function filter(array $displayers, Exception $original, Exception $transformed)
{
if ($this->config->get('app.debug', false) !== true) {
foreach ($displayers as $index => $displayer) {
Expand Down
3 changes: 2 additions & 1 deletion tests/Displayers/DebugDisplayerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ public function testClientError()

public function testProperties()
{
$exception = new Exception();
$displayer = new DebugDisplayer();

$this->assertTrue($displayer->isVerbose());
$this->assertTrue($displayer->canDisplay(new Exception()));
$this->assertTrue($displayer->canDisplay($exception, $exception));
$this->assertSame('text/html', $displayer->contentType());
}
}
3 changes: 2 additions & 1 deletion tests/Displayers/HtmlDisplayerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace GrahamCampbell\Tests\Exceptions\Displayers;

use Exception;
use GrahamCampbell\Exceptions\Displayers\HtmlDisplayer;
use GrahamCampbell\Exceptions\ExceptionInfo;
use GrahamCampbell\Tests\Exceptions\AbstractTestCase;
Expand Down Expand Up @@ -54,7 +55,7 @@ public function testProperties()
$displayer = new HtmlDisplayer(new ExceptionInfo(__DIR__.'/../../resources/errors.json'), __DIR__.'/../../resources/error.html');

$this->assertFalse($displayer->isVerbose());
$this->assertTrue($displayer->canDisplay(new HttpException(500)));
$this->assertTrue($displayer->canDisplay(new Exception(), new HttpException(500)));
$this->assertSame('text/html', $displayer->contentType());
}
}
3 changes: 2 additions & 1 deletion tests/Displayers/JsonDisplayerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use GrahamCampbell\Exceptions\Displayers\JsonDisplayer;
use GrahamCampbell\Exceptions\ExceptionInfo;
use GrahamCampbell\TestBench\AbstractTestCase;
use InvalidArgumentException;
use Symfony\Component\HttpKernel\Exception\HttpException;

/**
Expand Down Expand Up @@ -54,7 +55,7 @@ public function testProperties()
$displayer = new JsonDisplayer(new ExceptionInfo(__DIR__.'/../../resources/errors.json'));

$this->assertFalse($displayer->isVerbose());
$this->assertTrue($displayer->canDisplay(new HttpException(500)));
$this->assertTrue($displayer->canDisplay(new InvalidArgumentException(), new HttpException(500)));
$this->assertSame('application/json', $displayer->contentType());
}
}
12 changes: 6 additions & 6 deletions tests/Filters/CanDisplayFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ public function testFirstIsRemoved()
{
$exception = new Exception();
$html = Mockery::mock(HtmlDisplayer::class);
$html->shouldReceive('canDisplay')->once()->with($exception)->andReturn(false);
$html->shouldReceive('canDisplay')->once()->with($exception, $exception)->andReturn(false);
$json = Mockery::mock(JsonDisplayer::class);
$json->shouldReceive('canDisplay')->once()->with($exception)->andReturn(true);
$json->shouldReceive('canDisplay')->once()->with($exception, $exception)->andReturn(true);

$displayers = (new CanDisplayFilter())->filter([$html, $json], $exception);
$displayers = (new CanDisplayFilter())->filter([$html, $json], $exception, $exception);

$this->assertSame([$json], $displayers);
}
Expand All @@ -42,11 +42,11 @@ public function testNoChange()
{
$exception = new Exception();
$html = Mockery::mock(HtmlDisplayer::class);
$html->shouldReceive('canDisplay')->once()->with($exception)->andReturn(true);
$html->shouldReceive('canDisplay')->once()->with($exception, $exception)->andReturn(true);
$json = Mockery::mock(JsonDisplayer::class);
$json->shouldReceive('canDisplay')->once()->with($exception)->andReturn(true);
$json->shouldReceive('canDisplay')->once()->with($exception, $exception)->andReturn(true);

$displayers = (new CanDisplayFilter())->filter([$html, $json], $exception);
$displayers = (new CanDisplayFilter())->filter([$html, $json], $exception, $exception);

$this->assertSame([$html, $json], $displayers);
}
Expand Down
Loading

0 comments on commit 001880b

Please sign in to comment.