Skip to content

Commit

Permalink
Added response body to HttpServerException.
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul committed Mar 9, 2017
1 parent 5dfd15b commit 536c748
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/Net/Http/HttpConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ public function fetchFreshData($source, EncapsulatedOptions $options = null)
if ($code < 200 || $code >= 400) {
throw new HttpServerException(
"HTTP server responded with error: \"$http_response_header[0]\".\n\n$response",
$code
$code,
$response
);
}

Expand Down
29 changes: 28 additions & 1 deletion src/Net/Http/HttpServerException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,32 @@
*/
class HttpServerException extends RecoverableConnectorException
{
// Intentionally empty.
/**
* @var string Response body.
*/
private $body;

/**
* Initializes this instance with the specified HTTP error message, HTTP response code and response body.
*
* @param string $message HTTP error message.
* @param int $code HTP response code.
* @param string $body Response body.
*/
public function __construct($message, $code, $body)
{
parent::__construct($message, $code);

$this->body = "$body";
}

/**
* Gets the response body.
*
* @return string Response body.
*/
public function getBody()
{
return $this->body;
}
}
4 changes: 4 additions & 0 deletions test/Functional/Porter/Net/Http/HttpConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ public function testErrorResponse()

try {
$this->fetch();
} catch (HttpServerException $exception) {
$this->assertSame('foo', $exception->getBody());

throw $exception;
} finally {
$this->stopServer($server);
}
Expand Down
27 changes: 26 additions & 1 deletion test/Unit/Porter/Net/Http/HttpServerExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,33 @@

final class HttpServerExceptionTest extends \PHPUnit_Framework_TestCase
{
/**
* @var HttpServerException
*/
private $exception;

protected function setUp()
{
$this->exception = new HttpServerException('foo', 123, 'bar');
}

public function testRecoverable()
{
self::assertInstanceOf(RecoverableConnectorException::class, new HttpServerException);
self::assertInstanceOf(RecoverableConnectorException::class, $this->exception);
}

public function testMessage()
{
$this->assertSame('foo', $this->exception->getMessage());
}

public function testCode()
{
$this->assertSame(123, $this->exception->getCode());
}

public function testBody()
{
self::assertSame('bar', $this->exception->getBody());
}
}

0 comments on commit 536c748

Please sign in to comment.