Skip to content

Commit

Permalink
improve output (#7)
Browse files Browse the repository at this point in the history
* improve output

* add uri
  • Loading branch information
nick-zh authored Oct 26, 2020
1 parent aa811b8 commit 0249ed6
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 7 deletions.
3 changes: 0 additions & 3 deletions docker/dev/php/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,4 @@ RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin -

USER www-data

# COMPOSER: install dependencies
RUN composer global require hirak/prestissimo

WORKDIR /var/www/html
7 changes: 6 additions & 1 deletion src/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class ErrorHandler implements ErrorHandlerInterface
{
/**
* @param ResponseInterface $response
* @param string|null $uri
* @return void
* @throws BackendDatastoreException
* @throws ClientException
Expand All @@ -40,7 +41,7 @@ class ErrorHandler implements ErrorHandlerInterface
* @throws VersionNotFoundException
* @throws ImportException
*/
public function handleError(ResponseInterface $response): void
public function handleError(ResponseInterface $response, string $uri = null): void
{
$responseContent = json_decode($response->getBody(), true, 512, JSON_THROW_ON_ERROR);

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

if (null !== $uri) {
$message .= sprintf(' (%s)', $uri);
}

switch ($code) {
case 50001:
throw new BackendDatastoreException($message);
Expand Down
3 changes: 2 additions & 1 deletion src/ErrorHandlerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ interface ErrorHandlerInterface
{
/**
* @param ResponseInterface $response
* @param string|null $uri
*/
public function handleError(ResponseInterface $response): void;
public function handleError(ResponseInterface $response, string $uri = null): void;
}
2 changes: 1 addition & 1 deletion src/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public function call(string $method, string $uri, array $body = [], array $query
{
$response = $this->client->sendRequest($this->createRequest($method, $uri, $body, $queryParams));

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

return json_decode($response->getBody(), true, 512, JSON_THROW_ON_ERROR);
}
Expand Down
16 changes: 15 additions & 1 deletion tests/ErrorHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,20 @@ public function testExceptionThrow(?int $code, string $expectedException): void
$errorHandler->handleError($responseMock);
}

public function testExceptionThrowWithUri(): void
{
/** @var ResponseInterface|MockObject $responseMock */
$responseMock = $this->makeResponseInterfaceMock(50001, self::TEST_MESSAGE);

$errorHandler = new ErrorHandler();

$this->expectException(BackendDatastoreException::class);
$this->expectExceptionMessage(self::TEST_MESSAGE . sprintf(' (%s)', 'http://test.com'));

$errorHandler->handleError($responseMock, 'http://test.com');
}


public function testNoExceptionIfNoErrorCode(): void
{
/** @var ResponseInterface|MockObject $responseMock */
Expand All @@ -121,4 +135,4 @@ public function testNoExceptionIfNoErrorCode(): void
// It sounds weird, trust me...
$this->assertTrue(true);
}
}
}

0 comments on commit 0249ed6

Please sign in to comment.