Commit 72173059 authored by Bas de Nooijer's avatar Bas de Nooijer

- merged branch 'add-body-to-http-exception' of github.com:m0ppers/solarium into develop

- added some small fixes
- added unittests

Conflicts:
	library/Solarium/Exception/HttpException.php
parents a77e8078 a6f4be2a
......@@ -103,7 +103,8 @@ class Result implements ResultInterface
if ($statusNum == 4 || $statusNum == 5) {
throw new HttpException(
$response->getStatusMessage(),
$response->getStatusCode()
$response->getStatusCode(),
$response->getBody()
);
}
}
......
......@@ -63,6 +63,15 @@ class HttpException extends \RuntimeException implements ExceptionInterface
*/
protected $statusMessage;
/**
* HTTP response body
*
* Usually contains a description of the error (if Solr returned one)
*
* @var string
*/
protected $body;
/**
* Exception constructor
*
......@@ -71,17 +80,23 @@ class HttpException extends \RuntimeException implements ExceptionInterface
* more descriptive text. The original message is available using the
* {@link getStatusMessage} method.
*
* @param string $statusMessage
* @param int|null $code
* @param string $statusMessage
* @param int|null $code
* @param string|null $body
*/
public function __construct($statusMessage, $code = null)
public function __construct($statusMessage, $code = null, $body = null)
{
$this->statusMessage = $statusMessage;
$this->body = $body;
$message = 'Solr HTTP error: ' . $statusMessage;
if (null !== $code) {
$message .= ' (' . $code . ')';
}
if ($body) {
$message .= "\n" . $body;
}
parent::__construct($message, $code);
}
......@@ -96,4 +111,9 @@ class HttpException extends \RuntimeException implements ExceptionInterface
return $this->statusMessage;
}
public function getBody()
{
return $this->body;
}
}
......@@ -55,7 +55,17 @@ class HttpExceptionTest extends \PHPUnit_Framework_TestCase
);
}
public function testConstructorNoCode()
public function testGetBody()
{
$exception = new HttpException('message text', 123, 'body text');
$this->assertEquals(
'body text',
$exception->getBody()
);
}
public function testConstructorNoCodeOrBody()
{
$exception = new HttpException('message text');
......
......@@ -34,6 +34,7 @@ use Solarium\Core\Client\Client;
use Solarium\Core\Client\Response;
use Solarium\Core\Query\Result\Result;
use Solarium\QueryType\Select\Query\Query as SelectQuery;
use Solarium\Exception\HttpException;
class ResultTest extends \PHPUnit_Framework_TestCase
{
......@@ -59,12 +60,24 @@ class ResultTest extends \PHPUnit_Framework_TestCase
public function testResultWithErrorResponse()
{
$headers = array('HTTP/1.0 404 Not Found');
$response = new Response('', $headers);
$response = new Response('Error message', $headers);
$this->setExpectedException('Solarium\Exception\HttpException');
new Result($this->client, $this->query, $response);
}
public function testExceptionGetBody()
{
$headers = array('HTTP/1.0 404 Not Found');
$response = new Response('Error message', $headers);
try{
new Result($this->client, $this->query, $response);
}catch(HttpException $e){
$this->assertEquals('Error message', $e->getBody());
}
}
public function testGetResponse()
{
$this->assertEquals($this->response, $this->result->getResponse());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment