Skip to content

Commit

Permalink
Merge branch 'MAR-6868-error-logging' into 'master'
Browse files Browse the repository at this point in the history
[MAR-6868] Updated error logging, to log full error message

Closes MAR-6868

See merge request marketplace-team/marketplace-api-client!44
  • Loading branch information
michalsalon-mall committed Sep 18, 2019
2 parents c8ead5c + 6be8b8f commit 1965e3c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Marketplace API client - change log

## 3.11.1
- updated logging of request errors, they are no longer truncated after 120 characters

## 3.11.0
- added support for new `pricing` endpoints for Products and Variants

Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"name" : "mallgroup/mpapi-client",
"description" : "Mall marketplace API client",
"version" : "3.11.0",
"version" : "3.11.1",
"require" : {
"php" : "^5.5 || ^7.0",
"guzzlehttp/guzzle" : ">=6.2.3",
"psr/log" : "~1.0.2",
"ext-json": "*"
"ext-json": "*",
"ext-bcmath": "*"
},
"require-dev" : {
"codeception/codeception" : "~2.1",
Expand Down
18 changes: 16 additions & 2 deletions src/Services/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\Request;
use MPAPI\Entity\Paging;
Expand All @@ -27,7 +28,7 @@ class Client
*
* @var string
*/
const APPLICATION_NAME = 'mpapic-v3.11.0';
const APPLICATION_NAME = 'mpapic-v3.11.1';

/**
*
Expand Down Expand Up @@ -291,7 +292,8 @@ public function sendRequest($path, $method, array $body = [], array $args = [])
'body' => $body,
'client_id' => $this->clientId
]);
$responseData = json_decode($e->getResponse()->getBody()->getContents(), true);
$response = $e->getResponse()->getBody()->getContents();
$responseData = json_decode($response, true);
$e->getResponse()->getBody()->rewind();
$responseData = isset($responseData['data']) ? $responseData['data'] : $responseData;
if (isset($responseData['forceToken']) || isset($responseData['data']['forceToken'])) {
Expand All @@ -301,6 +303,18 @@ public function sendRequest($path, $method, array $body = [], array $args = [])
$exception->setForceToken($forceToken);
throw $exception;
}

// RequestException truncates the output to 120 chars., which does not fit some of our error messages (ie. media Content-Length)
if ($e instanceof RequestException) {
$message = $e->getMessage();
$pos = strpos($message, "` response:\n");
if ($pos !== false) {
// Replace the truncated response with full contents of the body
$message = substr_replace($message, $response . "\n", $pos + 12);
throw new \Exception($message, $e->getCode(), $e);
}
}

throw $e;
}
return $this->lastResponse;
Expand Down

0 comments on commit 1965e3c

Please sign in to comment.