Skip to content

Commit 536c748

Browse files
author
Paul
committed
Added response body to HttpServerException.
1 parent 5dfd15b commit 536c748

4 files changed

Lines changed: 60 additions & 3 deletions

File tree

src/Net/Http/HttpConnector.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ public function fetchFreshData($source, EncapsulatedOptions $options = null)
6969
if ($code < 200 || $code >= 400) {
7070
throw new HttpServerException(
7171
"HTTP server responded with error: \"$http_response_header[0]\".\n\n$response",
72-
$code
72+
$code,
73+
$response
7374
);
7475
}
7576

src/Net/Http/HttpServerException.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,32 @@
88
*/
99
class HttpServerException extends RecoverableConnectorException
1010
{
11-
// Intentionally empty.
11+
/**
12+
* @var string Response body.
13+
*/
14+
private $body;
15+
16+
/**
17+
* Initializes this instance with the specified HTTP error message, HTTP response code and response body.
18+
*
19+
* @param string $message HTTP error message.
20+
* @param int $code HTP response code.
21+
* @param string $body Response body.
22+
*/
23+
public function __construct($message, $code, $body)
24+
{
25+
parent::__construct($message, $code);
26+
27+
$this->body = "$body";
28+
}
29+
30+
/**
31+
* Gets the response body.
32+
*
33+
* @return string Response body.
34+
*/
35+
public function getBody()
36+
{
37+
return $this->body;
38+
}
1239
}

test/Functional/Porter/Net/Http/HttpConnectorTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ public function testErrorResponse()
5454

5555
try {
5656
$this->fetch();
57+
} catch (HttpServerException $exception) {
58+
$this->assertSame('foo', $exception->getBody());
59+
60+
throw $exception;
5761
} finally {
5862
$this->stopServer($server);
5963
}

test/Unit/Porter/Net/Http/HttpServerExceptionTest.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,33 @@
66

77
final class HttpServerExceptionTest extends \PHPUnit_Framework_TestCase
88
{
9+
/**
10+
* @var HttpServerException
11+
*/
12+
private $exception;
13+
14+
protected function setUp()
15+
{
16+
$this->exception = new HttpServerException('foo', 123, 'bar');
17+
}
18+
919
public function testRecoverable()
1020
{
11-
self::assertInstanceOf(RecoverableConnectorException::class, new HttpServerException);
21+
self::assertInstanceOf(RecoverableConnectorException::class, $this->exception);
22+
}
23+
24+
public function testMessage()
25+
{
26+
$this->assertSame('foo', $this->exception->getMessage());
27+
}
28+
29+
public function testCode()
30+
{
31+
$this->assertSame(123, $this->exception->getCode());
32+
}
33+
34+
public function testBody()
35+
{
36+
self::assertSame('bar', $this->exception->getBody());
1237
}
1338
}

0 commit comments

Comments
 (0)