Skip to content

Commit 970e674

Browse files
authoredNov 2, 2020
add request body to exception messge (#8)
1 parent 0249ed6 commit 970e674

7 files changed

+51
-10
lines changed
 

‎src/ErrorHandler.php

+9-3
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717
use Jobcloud\Kafka\SchemaRegistryClient\Exception\UnauthorizedException;
1818
use Jobcloud\Kafka\SchemaRegistryClient\Exception\UnprocessableEntityException;
1919
use Jobcloud\Kafka\SchemaRegistryClient\Exception\VersionNotFoundException;
20+
use Psr\Http\Message\RequestInterface;
2021
use Psr\Http\Message\ResponseInterface;
2122

2223
class ErrorHandler implements ErrorHandlerInterface
2324
{
2425
/**
25-
* @param ResponseInterface $response
26-
* @param string|null $uri
26+
* @param ResponseInterface $response
27+
* @param string|null $uri
28+
* @param RequestInterface|null $request
2729
* @return void
2830
* @throws BackendDatastoreException
2931
* @throws ClientException
@@ -41,7 +43,7 @@ class ErrorHandler implements ErrorHandlerInterface
4143
* @throws VersionNotFoundException
4244
* @throws ImportException
4345
*/
44-
public function handleError(ResponseInterface $response, string $uri = null): void
46+
public function handleError(ResponseInterface $response, string $uri = null, RequestInterface $request = null): void
4547
{
4648
$responseContent = json_decode($response->getBody(), true, 512, JSON_THROW_ON_ERROR);
4749

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

61+
if (null !== $request) {
62+
$message .= sprintf(' with request body: %s', $request->getBody()->getContents());
63+
}
64+
5965
switch ($code) {
6066
case 50001:
6167
throw new BackendDatastoreException($message);

‎src/ErrorHandlerInterface.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Jobcloud\Kafka\SchemaRegistryClient;
44

5+
use Psr\Http\Message\RequestInterface;
56
use Psr\Http\Message\ResponseInterface;
67

78
interface ErrorHandlerInterface
@@ -10,5 +11,9 @@ interface ErrorHandlerInterface
1011
* @param ResponseInterface $response
1112
* @param string|null $uri
1213
*/
13-
public function handleError(ResponseInterface $response, string $uri = null): void;
14+
public function handleError(
15+
ResponseInterface $response,
16+
string $uri = null,
17+
RequestInterface $request = null
18+
): void;
1419
}

‎src/HttpClient.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,11 @@ private function createRequest(
114114
*/
115115
public function call(string $method, string $uri, array $body = [], array $queryParams = [])
116116
{
117-
$response = $this->client->sendRequest($this->createRequest($method, $uri, $body, $queryParams));
117+
$request = $this->createRequest($method, $uri, $body, $queryParams);
118118

119-
$this->errorHandler->handleError($response, $uri);
119+
$response = $this->client->sendRequest($request);
120+
121+
$this->errorHandler->handleError($response, $uri, $request);
120122

121123
return json_decode($response->getBody(), true, 512, JSON_THROW_ON_ERROR);
122124
}

‎tests/ErrorHandlerTest.php

+20
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,13 @@
2020
use Jobcloud\Kafka\SchemaRegistryClient\Exception\VersionNotFoundException;
2121
use PHPUnit\Framework\MockObject\MockObject;
2222
use PHPUnit\Framework\TestCase;
23+
use Psr\Http\Message\RequestInterface;
2324
use Psr\Http\Message\ResponseInterface;
2425
use Psr\Http\Message\StreamInterface;
2526

27+
/**
28+
* @covers \Jobcloud\Kafka\SchemaRegistryClient\ErrorHandler
29+
*/
2630
class ErrorHandlerTest extends TestCase
2731
{
2832
private const TEST_MESSAGE = 'Test Message';
@@ -123,6 +127,22 @@ public function testExceptionThrowWithUri(): void
123127
$errorHandler->handleError($responseMock, 'http://test.com');
124128
}
125129

130+
public function testExceptionThrowWithRequest(): void
131+
{
132+
/** @var ResponseInterface|MockObject $responseMock */
133+
$responseMock = $this->makeResponseInterfaceMock(50001, self::TEST_MESSAGE);
134+
$requestMock = $this->getMockForAbstractClass(RequestInterface::class);
135+
$streamMock = $this->getMockForAbstractClass(StreamInterface::class);
136+
$streamMock->expects(self::once())->method('getContents')->willReturn('test body');
137+
$requestMock->expects(self::once())->method('getBody')->willReturn($streamMock);
138+
139+
$errorHandler = new ErrorHandler();
140+
141+
$this->expectException(BackendDatastoreException::class);
142+
$this->expectExceptionMessage(self::TEST_MESSAGE . sprintf(' (%s) with request body: %s', 'http://test.com', 'test body'));
143+
echo 'asdfasf';
144+
$errorHandler->handleError($responseMock, 'http://test.com', $requestMock);
145+
}
126146

127147
public function testNoExceptionIfNoErrorCode(): void
128148
{

‎tests/HttpClientTest.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
use Psr\Http\Message\ResponseInterface;
1313
use Psr\Http\Message\StreamInterface;
1414

15-
15+
/**
16+
* @covers \Jobcloud\Kafka\SchemaRegistryClient\HttpClient
17+
*/
1618
class HttpClientTest extends TestCase
1719
{
1820
use ReflectionAccessTrait;
@@ -162,4 +164,4 @@ public function testCallMethodWithThrownException(): void
162164
$this->expectException(Exception::class);
163165
$httpClient->call('GET', 'uri');
164166
}
165-
}
167+
}

‎tests/KafkaSchemaRegistryApiClientProviderTest.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
use Pimple\Container;
1212
use Psr\Http\Message\RequestFactoryInterface;
1313

14+
/**
15+
* @covers \Jobcloud\Kafka\SchemaRegistryClient\ServiceProvider\KafkaSchemaRegistryApiClientProvider
16+
*/
1417
class KafkaSchemaRegistryApiClientProviderTest extends TestCase
1518
{
1619
use ReflectionAccessTrait;
@@ -93,4 +96,4 @@ public function testUserNameAndPasswordFromSettingsArePassedToHttpClient(): void
9396
);
9497

9598
}
96-
}
99+
}

‎tests/KafkaSchemaRegistryApiApiClientTest.php ‎tests/KafkaSchemaRegistryApiClientTest.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
use PHPUnit\Framework\MockObject\MockObject;
1313
use PHPUnit\Framework\TestCase;
1414

15+
/**
16+
* @covers \Jobcloud\Kafka\SchemaRegistryClient\KafkaSchemaRegistryApiClient
17+
*/
1518
class KafkaSchemaRegistryApiApiClientTest extends TestCase
1619
{
1720
private const TEST_SUBJECT_NAME = 'some-subject';
@@ -438,4 +441,4 @@ public function testImportModeSuccess(): void
438441
$result = $api->setImportMode('ABC');
439442
self::assertTrue($result);
440443
}
441-
}
444+
}

0 commit comments

Comments
 (0)