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

Commit

Permalink
Fix "500 OK" error responses
Browse files Browse the repository at this point in the history
If the current response already has a status code that corresponds to
the exception that was encountered, it is assumed the developer dealt
with the reason phrase as well, and the reason phrase will be preserved.
If not, the standard message for the resulting HTTP status code will be
used.
  • Loading branch information
michaelmoussa committed Mar 22, 2016
1 parent 0c5903a commit e7d1b83
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
11 changes: 6 additions & 5 deletions src/FinalHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,13 @@ public function setOriginalResponse(ResponseInterface $response = null)
*/
private function handleError($error, RequestInterface $request, ResponseInterface $response)
{
$response = $response->withStatus(
Utils::getStatusCode($error, $response),
$response->getReasonPhrase()
);

$statusCode = Utils::getStatusCode($error, $response);
$reasonPhrase = $response->getStatusCode() === $statusCode
? $response->getReasonPhrase()
: '';
$response = $response->withStatus($statusCode, $reasonPhrase);
$message = $response->getReasonPhrase() ?: 'Unknown Error';

if (isset($this->options['env']) && $this->options['env'] !== 'production') {
$message = $this->createDevelopmentErrorMessage($error);
}
Expand Down
30 changes: 28 additions & 2 deletions test/FinalHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,26 @@

class FinalHandlerTest extends TestCase
{
/**
* @var Escaper
*/
private $escaper;

/**
* @var FinalHandler
*/
private $final;

/**
* @var Request
*/
private $request;

/**
* @var Response
*/
private $response;

public function setUp()
{
$psrRequest = new PsrRequest([], [], 'http://example.com/', 'GET', 'php://memory');
Expand Down Expand Up @@ -149,13 +169,19 @@ public function testCreates404ResponseWhenNoErrorIsPresent()
$this->assertEquals(404, $response->getStatusCode());
}

public function testErrorResponsePreservesOriginalReasonPhraseIfSet()
public function testErrorResponsePreservesResponseReasonPhraseIfStatusCodeMatchesExceptionCode()
{
$this->response = $this->response->withStatus(500, 'It broke!');
$response = call_user_func($this->final, $this->request, $this->response, new \Exception('foo'));
$response = call_user_func($this->final, $this->request, $this->response, new \Exception('foo', 500));
$this->assertSame($this->response->getReasonPhrase(), $response->getReasonPhrase());
}

public function testErrorResponseUsesStandardHttpStatusCodeReasonPhraseIfExceptionCodeCausesStatusCodeToChange()
{
$response = call_user_func($this->final, $this->request, $this->response, new \Exception('foo', 418));
$this->assertSame("I'm a teapot", $response->getReasonPhrase());
}

public function test404ResponseIncludesOriginalRequestUri()
{
$originalUrl = 'http://local.example.com/bar/foo';
Expand Down

0 comments on commit e7d1b83

Please sign in to comment.