diff --git a/src/Net/Http/HttpConnector.php b/src/Net/Http/HttpConnector.php index 5b0af53..b7462a6 100644 --- a/src/Net/Http/HttpConnector.php +++ b/src/Net/Http/HttpConnector.php @@ -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 ); } diff --git a/src/Net/Http/HttpServerException.php b/src/Net/Http/HttpServerException.php index 915bcca..871cb7d 100644 --- a/src/Net/Http/HttpServerException.php +++ b/src/Net/Http/HttpServerException.php @@ -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; + } } diff --git a/test/Functional/Porter/Net/Http/HttpConnectorTest.php b/test/Functional/Porter/Net/Http/HttpConnectorTest.php index eeb93b1..277c7dd 100644 --- a/test/Functional/Porter/Net/Http/HttpConnectorTest.php +++ b/test/Functional/Porter/Net/Http/HttpConnectorTest.php @@ -54,6 +54,10 @@ public function testErrorResponse() try { $this->fetch(); + } catch (HttpServerException $exception) { + $this->assertSame('foo', $exception->getBody()); + + throw $exception; } finally { $this->stopServer($server); } diff --git a/test/Unit/Porter/Net/Http/HttpServerExceptionTest.php b/test/Unit/Porter/Net/Http/HttpServerExceptionTest.php index d349339..1fc253b 100644 --- a/test/Unit/Porter/Net/Http/HttpServerExceptionTest.php +++ b/test/Unit/Porter/Net/Http/HttpServerExceptionTest.php @@ -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()); } }