Skip to content

Commit 0249ed6

Browse files
authored
improve output (#7)
* improve output * add uri
1 parent aa811b8 commit 0249ed6

File tree

5 files changed

+24
-7
lines changed

5 files changed

+24
-7
lines changed

docker/dev/php/Dockerfile

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,4 @@ RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin -
2626

2727
USER www-data
2828

29-
# COMPOSER: install dependencies
30-
RUN composer global require hirak/prestissimo
31-
3229
WORKDIR /var/www/html

src/ErrorHandler.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class ErrorHandler implements ErrorHandlerInterface
2323
{
2424
/**
2525
* @param ResponseInterface $response
26+
* @param string|null $uri
2627
* @return void
2728
* @throws BackendDatastoreException
2829
* @throws ClientException
@@ -40,7 +41,7 @@ class ErrorHandler implements ErrorHandlerInterface
4041
* @throws VersionNotFoundException
4142
* @throws ImportException
4243
*/
43-
public function handleError(ResponseInterface $response): void
44+
public function handleError(ResponseInterface $response, string $uri = null): void
4445
{
4546
$responseContent = json_decode($response->getBody(), true, 512, JSON_THROW_ON_ERROR);
4647

@@ -51,6 +52,10 @@ public function handleError(ResponseInterface $response): void
5152
$code = $responseContent['error_code'];
5253
$message = $responseContent['message'] ?? '';
5354

55+
if (null !== $uri) {
56+
$message .= sprintf(' (%s)', $uri);
57+
}
58+
5459
switch ($code) {
5560
case 50001:
5661
throw new BackendDatastoreException($message);

src/ErrorHandlerInterface.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ interface ErrorHandlerInterface
88
{
99
/**
1010
* @param ResponseInterface $response
11+
* @param string|null $uri
1112
*/
12-
public function handleError(ResponseInterface $response): void;
13+
public function handleError(ResponseInterface $response, string $uri = null): void;
1314
}

src/HttpClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public function call(string $method, string $uri, array $body = [], array $query
116116
{
117117
$response = $this->client->sendRequest($this->createRequest($method, $uri, $body, $queryParams));
118118

119-
$this->errorHandler->handleError($response);
119+
$this->errorHandler->handleError($response, $uri);
120120

121121
return json_decode($response->getBody(), true, 512, JSON_THROW_ON_ERROR);
122122
}

tests/ErrorHandlerTest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,20 @@ public function testExceptionThrow(?int $code, string $expectedException): void
110110
$errorHandler->handleError($responseMock);
111111
}
112112

113+
public function testExceptionThrowWithUri(): void
114+
{
115+
/** @var ResponseInterface|MockObject $responseMock */
116+
$responseMock = $this->makeResponseInterfaceMock(50001, self::TEST_MESSAGE);
117+
118+
$errorHandler = new ErrorHandler();
119+
120+
$this->expectException(BackendDatastoreException::class);
121+
$this->expectExceptionMessage(self::TEST_MESSAGE . sprintf(' (%s)', 'http://test.com'));
122+
123+
$errorHandler->handleError($responseMock, 'http://test.com');
124+
}
125+
126+
113127
public function testNoExceptionIfNoErrorCode(): void
114128
{
115129
/** @var ResponseInterface|MockObject $responseMock */
@@ -121,4 +135,4 @@ public function testNoExceptionIfNoErrorCode(): void
121135
// It sounds weird, trust me...
122136
$this->assertTrue(true);
123137
}
124-
}
138+
}

0 commit comments

Comments
 (0)