Skip to content

Commit

Permalink
✨ exception handler now aware of request
Browse files Browse the repository at this point in the history
  • Loading branch information
carbontwelve committed Dec 4, 2017
1 parent c0aa3ff commit 3d27878
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public function dispatch(ServerRequest $request = null, $catchesExceptions = tru
}

$handler = $this->exceptionHandler;
$response = $handler($e);
$response = $handler($e, $request);

if (!$response instanceof \Psr\Http\Message\ResponseInterface) {
throw new InvalidHandlerResponseException('The exception handler ['.get_class($handler).'] did not return a valid response.');
Expand Down
7 changes: 7 additions & 0 deletions src/ErrorHandlers/DefaultExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ class DefaultExceptionHandler implements ExceptionHandler
*/
protected $ignore = [];

/**
* @param string $class
*/
public function addIgnored($class) {
array_push($this->ignore, $class);
}

/**
* @param Exception|RouteNotFoundException|ContainerNotFoundException $e
* @param RequestInterface $request
Expand Down
5 changes: 4 additions & 1 deletion src/ErrorHandlers/ExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

namespace Photogabble\Tuppence\ErrorHandlers;

use Psr\Http\Message\RequestInterface;

interface ExceptionHandler
{
/**
* @param \Exception $e
* @param RequestInterface $request
* @return \Psr\Http\Message\ResponseInterface
*/
public function __invoke(\Exception $e);
public function __invoke(\Exception $e, RequestInterface $request);
}

11 changes: 9 additions & 2 deletions tests/Unit/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Photogabble\Tuppence\ErrorHandlers\DefaultExceptionHandler;
use Photogabble\Tuppence\ErrorHandlers\InvalidHandlerException;
use Photogabble\Tuppence\ErrorHandlers\InvalidHandlerResponseException;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Photogabble\Tuppence\Tests\TestEmitter;
Expand Down Expand Up @@ -126,7 +127,10 @@ public function testExceptionHandlerFailsWhenNotValidResponse()
$exceptionMock = $this->createMock(DefaultExceptionHandler::class);
$exceptionMock->expects($this->once())
->method('__invoke')
->with($this->isInstanceOf(\League\Route\Http\Exception\NotFoundException::class));
->with(
$this->isInstanceOf(\League\Route\Http\Exception\NotFoundException::class),
$this->isInstanceOf(RequestInterface::class)
);

$emitter = new TestEmitter();
$app = new App($emitter);
Expand All @@ -148,7 +152,10 @@ public function testExceptionHandler()

$exceptionMock->expects($this->once())
->method('__invoke')
->with($this->isInstanceOf(\League\Route\Http\Exception\NotFoundException::class));
->with(
$this->isInstanceOf(\League\Route\Http\Exception\NotFoundException::class),
$this->isInstanceOf(RequestInterface::class)
);

$emitter = new TestEmitter();
$app = new App($emitter);
Expand Down
38 changes: 38 additions & 0 deletions tests/Unit/DefaultExceptionHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Photogabble\Tuppence\Tests\Unit;

use \Exception;
use Photogabble\Tuppence\ErrorHandlers\DefaultExceptionHandler;
use Zend\Diactoros\Response\JsonResponse;
use Zend\Diactoros\ServerRequestFactory;

class DefaultExceptionHandlerTest extends \PHPUnit_Framework_TestCase
{
public function testResponse()
{
$handler = new DefaultExceptionHandler();
$exception = new Exception('Test');

$response = $handler($exception, ServerRequestFactory::fromGlobals());
$this->assertInstanceOf(JsonResponse::class, $response);

$jsonDecode = json_decode((string) $response->getBody()->getContents(), true);

$this->assertEquals('Test', $jsonDecode['message']);

}

public function testIgnoreFunctionality()
{
$handler = new DefaultExceptionHandler();
$handler->addIgnored(Exception::class);
$exception = new Exception('Test');

try {
$handler($exception, ServerRequestFactory::fromGlobals());
} catch (Exception $e) {
$this->assertSame($exception, $e);
}
}
}

0 comments on commit 3d27878

Please # to comment.