Skip to content

Commit

Permalink
Merge pull request #18 from equip/feature/http-status-exceptions
Browse files Browse the repository at this point in the history
Modify ExceptionHandler to support getHttpStatus
  • Loading branch information
elazar committed Feb 29, 2016
2 parents 35fe673 + e701702 commit 9cc5525
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Handler/ExceptionHandler.php
Expand Up @@ -71,7 +71,12 @@ public function __invoke(
$response = $response->withHeader('Content-Type', $type);

try {
$response = $response->withStatus($e->getCode());
if (method_exists($e, 'getHttpStatus')) {
$code = $e->getHttpStatus();
} else {
$code = $e->getCode();
}
$response = $response->withStatus($code);
} catch (InvalidArgumentException $_) {
// Exception did not contain a valid code
$response = $response->withStatus(500);
Expand Down
19 changes: 19 additions & 0 deletions tests/Handler/ExceptionHandlerTest.php
Expand Up @@ -63,6 +63,17 @@ public function testHandle($mime)
$this->assertEquals($mime, $response->getHeaderLine('Content-Type'));
}

public function testHandleWithHttpStatusCode()
{
$request = new ServerRequest;

$response = $this->execute(function ($request, $response) {
throw new ExceptionWithHttpStatusCode('foo');
}, $request);

$this->assertEquals(400, $response->getStatusCode());
}

public function testNotFound()
{
$response = $this->execute(function ($request, $response) {
Expand All @@ -82,3 +93,11 @@ public function testMethodNotAllowed()
$this->assertEquals('GET,PUT', $response->getHeaderLine('Allow'));
}
}

class ExceptionWithHttpStatusCode extends \Exception
{
public function getHttpStatus()
{
return 400;
}
}

0 comments on commit 9cc5525

Please sign in to comment.