diff --git a/src/Client.php b/src/Client.php index e497373ebe..71a5cd6a9d 100644 --- a/src/Client.php +++ b/src/Client.php @@ -242,6 +242,7 @@ public function execute(ClientRequestInterface $request) throw $exception; } $response = $exception->getResponse(); + $this->logException($exception); } $this->logDeprecatedRequest($response, $httpRequest); @@ -308,6 +309,7 @@ public function executeBatch() ) { throw $httpResponse; } + $this->logException($httpResponse); $httpResponse = $httpResponse->getResponse(); } $responses[$request->getIdentifier()] = $request->buildResponse($httpResponse); @@ -319,6 +321,30 @@ public function executeBatch() return $responses; } + /** + * @param $exception + * @return $this + */ + protected function logException(ApiException $exception) + { + if (is_null($this->logger)) { + return $this; + } + $response = $exception->getResponse(); + + $context = []; + if ($response instanceof ResponseInterface) { + $context = [ + 'responseStatusCode' => $response->getStatusCode(), + 'responseHeaders' => $response->getHeaders(), + 'responseBody' => (string)$response->getBody(), + ]; + } + $this->logger->error($exception->getMessage(), $context); + + return $this; + } + /** * @param ResponseInterface $response * @param RequestInterface $request diff --git a/tests/unit/ClientTest.php b/tests/unit/ClientTest.php index f4ee26b497..002983593e 100644 --- a/tests/unit/ClientTest.php +++ b/tests/unit/ClientTest.php @@ -355,6 +355,92 @@ public function testLogDeprecatedMethod() ); } + public function testLoggingBody() + { + $handler = new TestHandler(); + $logger = new Logger('test'); + $logger->pushHandler($handler); + + $errorBody = ' + { + "statusCode": 400, + "message": "Some error", + "errors": [ + { + "code": "InvalidOperation", + "message": "Some error" + } + ] + }'; + $client = $this->getMockClient( + $this->getConfig(), + $errorBody, + 400, + $logger + ); + + $endpoint = new JsonEndpoint('test'); + /** + * @var ClientRequestInterface $request + */ + $request = $this->getMockForAbstractClass( + '\Commercetools\Core\Request\AbstractByIdGetRequest', + [$endpoint, 'id'] + ); + $client->execute($request); + + $logEntry = $handler->getRecords()[1]; + $this->assertSame(Logger::ERROR, $logEntry['level']); + $this->assertSame( + 'Client error response [url] test/id [status code] 400 [reason phrase] Bad Request', + (string)$logEntry['message'] + ); + $this->assertJsonStringEqualsJsonString($errorBody, $logEntry['context']['responseBody']); + } + + public function testLoggingBatchBody() + { + $handler = new TestHandler(); + $logger = new Logger('test'); + $logger->pushHandler($handler); + + $errorBody = ' + { + "statusCode": 400, + "message": "Some error", + "errors": [ + { + "code": "InvalidOperation", + "message": "Some error" + } + ] + }'; + $client = $this->getMockClient( + $this->getConfig(), + $errorBody, + 400, + $logger + ); + + $endpoint = new JsonEndpoint('test'); + /** + * @var ClientRequestInterface $request + */ + $request = $this->getMockForAbstractClass( + '\Commercetools\Core\Request\AbstractByIdGetRequest', + [$endpoint, 'id'] + ); + $client->addBatchRequest($request); + $client->executeBatch(); + + $logEntry = $handler->getRecords()[1]; + $this->assertSame(Logger::ERROR, $logEntry['level']); + $this->assertSame( + 'Client error response [url] test/id [status code] 400 [reason phrase] Bad Request', + (string)$logEntry['message'] + ); + $this->assertJsonStringEqualsJsonString($errorBody, $logEntry['context']['responseBody']); + } /** * @expectedException \Commercetools\Core\Error\ApiException */