Skip to content

Commit

Permalink
add request body to exception messge (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-zh committed Nov 2, 2020
1 parent 0249ed6 commit 970e674
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 10 deletions.
12 changes: 9 additions & 3 deletions src/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
use Jobcloud\Kafka\SchemaRegistryClient\Exception\UnauthorizedException;
use Jobcloud\Kafka\SchemaRegistryClient\Exception\UnprocessableEntityException;
use Jobcloud\Kafka\SchemaRegistryClient\Exception\VersionNotFoundException;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

class ErrorHandler implements ErrorHandlerInterface
{
/**
* @param ResponseInterface $response
* @param string|null $uri
* @param ResponseInterface $response
* @param string|null $uri
* @param RequestInterface|null $request
* @return void
* @throws BackendDatastoreException
* @throws ClientException
Expand All @@ -41,7 +43,7 @@ class ErrorHandler implements ErrorHandlerInterface
* @throws VersionNotFoundException
* @throws ImportException
*/
public function handleError(ResponseInterface $response, string $uri = null): void
public function handleError(ResponseInterface $response, string $uri = null, RequestInterface $request = null): void
{
$responseContent = json_decode($response->getBody(), true, 512, JSON_THROW_ON_ERROR);

Expand All @@ -56,6 +58,10 @@ public function handleError(ResponseInterface $response, string $uri = null): vo
$message .= sprintf(' (%s)', $uri);
}

if (null !== $request) {
$message .= sprintf(' with request body: %s', $request->getBody()->getContents());
}

switch ($code) {
case 50001:
throw new BackendDatastoreException($message);
Expand Down
7 changes: 6 additions & 1 deletion src/ErrorHandlerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Jobcloud\Kafka\SchemaRegistryClient;

use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

interface ErrorHandlerInterface
Expand All @@ -10,5 +11,9 @@ interface ErrorHandlerInterface
* @param ResponseInterface $response
* @param string|null $uri
*/
public function handleError(ResponseInterface $response, string $uri = null): void;
public function handleError(
ResponseInterface $response,
string $uri = null,
RequestInterface $request = null
): void;
}
6 changes: 4 additions & 2 deletions src/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,11 @@ private function createRequest(
*/
public function call(string $method, string $uri, array $body = [], array $queryParams = [])
{
$response = $this->client->sendRequest($this->createRequest($method, $uri, $body, $queryParams));
$request = $this->createRequest($method, $uri, $body, $queryParams);

$this->errorHandler->handleError($response, $uri);
$response = $this->client->sendRequest($request);

$this->errorHandler->handleError($response, $uri, $request);

return json_decode($response->getBody(), true, 512, JSON_THROW_ON_ERROR);
}
Expand Down
20 changes: 20 additions & 0 deletions tests/ErrorHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@
use Jobcloud\Kafka\SchemaRegistryClient\Exception\VersionNotFoundException;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;

/**
* @covers \Jobcloud\Kafka\SchemaRegistryClient\ErrorHandler
*/
class ErrorHandlerTest extends TestCase
{
private const TEST_MESSAGE = 'Test Message';
Expand Down Expand Up @@ -123,6 +127,22 @@ public function testExceptionThrowWithUri(): void
$errorHandler->handleError($responseMock, 'http://test.com');
}

public function testExceptionThrowWithRequest(): void
{
/** @var ResponseInterface|MockObject $responseMock */
$responseMock = $this->makeResponseInterfaceMock(50001, self::TEST_MESSAGE);
$requestMock = $this->getMockForAbstractClass(RequestInterface::class);
$streamMock = $this->getMockForAbstractClass(StreamInterface::class);
$streamMock->expects(self::once())->method('getContents')->willReturn('test body');
$requestMock->expects(self::once())->method('getBody')->willReturn($streamMock);

$errorHandler = new ErrorHandler();

$this->expectException(BackendDatastoreException::class);
$this->expectExceptionMessage(self::TEST_MESSAGE . sprintf(' (%s) with request body: %s', 'http://test.com', 'test body'));
echo 'asdfasf';
$errorHandler->handleError($responseMock, 'http://test.com', $requestMock);
}

public function testNoExceptionIfNoErrorCode(): void
{
Expand Down
6 changes: 4 additions & 2 deletions tests/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;


/**
* @covers \Jobcloud\Kafka\SchemaRegistryClient\HttpClient
*/
class HttpClientTest extends TestCase
{
use ReflectionAccessTrait;
Expand Down Expand Up @@ -162,4 +164,4 @@ public function testCallMethodWithThrownException(): void
$this->expectException(Exception::class);
$httpClient->call('GET', 'uri');
}
}
}
5 changes: 4 additions & 1 deletion tests/KafkaSchemaRegistryApiClientProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
use Pimple\Container;
use Psr\Http\Message\RequestFactoryInterface;

/**
* @covers \Jobcloud\Kafka\SchemaRegistryClient\ServiceProvider\KafkaSchemaRegistryApiClientProvider
*/
class KafkaSchemaRegistryApiClientProviderTest extends TestCase
{
use ReflectionAccessTrait;
Expand Down Expand Up @@ -93,4 +96,4 @@ public function testUserNameAndPasswordFromSettingsArePassedToHttpClient(): void
);

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

/**
* @covers \Jobcloud\Kafka\SchemaRegistryClient\KafkaSchemaRegistryApiClient
*/
class KafkaSchemaRegistryApiApiClientTest extends TestCase
{
private const TEST_SUBJECT_NAME = 'some-subject';
Expand Down Expand Up @@ -438,4 +441,4 @@ public function testImportModeSuccess(): void
$result = $api->setImportMode('ABC');
self::assertTrue($result);
}
}
}

0 comments on commit 970e674

Please sign in to comment.