Skip to content

Commit

Permalink
Simplifies default error handler
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobemerick committed Nov 2, 2016
1 parent 35f8479 commit d55d8e4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 85 deletions.
36 changes: 8 additions & 28 deletions src/Talus.php
Expand Up @@ -51,7 +51,7 @@ public function __construct(array $config)
$this->container = $config['container'];
}

$this->logger = new NullLogger();
$this->logger = new NullLogger;
// todo we probably shouldn't allow passing in logger as config key
if (!empty($config['logger'])) {
if (!($config['logger'] instanceof LoggerInterface)) {
Expand All @@ -60,6 +60,8 @@ public function __construct(array $config)
$this->logger = $config['logger'];
}

$this->errorHandler = new ErrorHandler;

if (empty($config['swagger'])) {
throw new DomainException('missing swagger information');
}
Expand Down Expand Up @@ -91,8 +93,8 @@ public function run()

try {
$result = $this->callStack($request, $response);
} catch (Exception $e) {
$result = $this->handleError($request, $response, $e);
} catch (Exception $exception) {
$this->errorHandler->__invoke($request, $response, $exception);
}

$this->outputResponse($result);
Expand Down Expand Up @@ -133,15 +135,9 @@ public function __invoke(RequestInterface $request, ResponseInterface $response)
$controllerName = $request->getAttribute('swagger')['path']['x-swagger-router-controller'];
$methodName = $request->getAttribute('swagger')['operation']['operationId'];

try {
// todo this should be container-controlled
$controller = new $controllerName($this->container);
return $controller->$methodName($request, $response);
} catch (Exception $e) {
// todo handle straight errors too
throw $e;
}

// todo this should be container-controlled
$controller = new $controllerName($this->container);
return $controller->$methodName($request, $response);
}

/**
Expand All @@ -159,20 +155,4 @@ protected function getResponse()
{
return new Response();
}

/**
* @param RequestInterface $request
* @param ResponseInterface $response
* @param Exception $exception
* @return ResponseInterface
*/
protected function handleError(RequestInterface $request, ResponseInterface $response, Exception $exception)
{
if (!isset($this->errorHandler)) {
$errorHandler = new ErrorHandler;
return $errorHandler->__invoke($request, $response, $exception);
}

return $this->errorHandler->__invoke($request, $response, $exception);
}
}
67 changes: 10 additions & 57 deletions tests/TalusTest.php
Expand Up @@ -7,6 +7,7 @@
use ReflectionClass;
use stdclass;

use AvalancheDevelopment\CrashPad\ErrorHandler;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
Expand Down Expand Up @@ -89,6 +90,15 @@ public function testConstructValidatesLogger()
]);
}

public function testConstructSetsErrorHandler()
{
$talus = new Talus([
'swagger' => ['swagger'],
]);

$this->assertAttributeInstanceOf(ErrorHandler::class, 'errorHandler', $talus);
}

/**
* @expectedException DomainException
* @expectedExceptionMessage missing swagger information
Expand Down Expand Up @@ -317,61 +327,4 @@ public function testGetResponse()

$this->assertInstanceOf('Psr\Http\Message\ResponseInterface', $response);
}

public function testHandleErrorDefault()
{
$this->markTestIncomplete('need to find a way to mock error handler');

$exception = new Exception('test error');

$request = $this->createMock(RequestInterface::class);
$response = $this->createMock(ResponseInterface::class);

$reflectedTalus = new ReflectionClass(Talus::class);
$reflectedHandleError = $reflectedTalus->getMethod('handleError');
$reflectedHandleError->setAccessible(true);

$talus = $this->getMockBuilder(Talus::class)
->disableOriginalConstructor()
->setMethods()
->getMock();

$result = $reflectedHandleError->invokeArgs($talus, [ $request, $response, $exception ]);
$this->assertInstanceOf('Psr\Http\Message\ResponseInterface', $result);
}

public function testHandleErrorCustom()
{
$exception = new Exception('test error');
$errorHandler = function ($req, $res, $exception) {
$res->getBody()->write("SOME ERROR: {$exception->getMessage()}");
return $res;
};

$stream = $this->createMock(StreamInterface::class);
$stream->expects($this->once())
->method('write')
->with('SOME ERROR: test error');

$request = $this->createMock(RequestInterface::class);
$response = $this->createMock(ResponseInterface::class);
$response->expects($this->once())
->method('getBody')
->willReturn($stream);

$reflectedTalus = new ReflectionClass(Talus::class);
$reflectedHandler = $reflectedTalus->getProperty('errorHandler');
$reflectedHandler->setAccessible(true);
$reflectedHandleError = $reflectedTalus->getMethod('handleError');
$reflectedHandleError->setAccessible(true);

$talus = $this->getMockBuilder(Talus::class)
->disableOriginalConstructor()
->setMethods()
->getMock();

$reflectedHandler->setValue($talus, $errorHandler);
$result = $reflectedHandleError->invokeArgs($talus, [ $request, $response, $exception ]);
$this->assertInstanceOf('Psr\Http\Message\ResponseInterface', $result);
}
}

0 comments on commit d55d8e4

Please sign in to comment.