Skip to content

Commit 6463d3d

Browse files
authored
Merge pull request #658 from aaajeetee/main
Log the response body if the json_encode() fails.
2 parents b13eea7 + 13b7f7b commit 6463d3d

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

src/Picqer/Financials/Exact/Connection.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -456,9 +456,10 @@ private function parseResponse(Response $response, $returnSingleIfPossible = tru
456456
}
457457

458458
Psr7\Message::rewindBody($response);
459-
$json = json_decode($response->getBody()->getContents(), true);
459+
$responseBody = $response->getBody()->getContents();
460+
$json = json_decode($responseBody, true);
460461
if (false === is_array($json)) {
461-
throw new ApiException('Json decode failed. Got response: ' . $response->getBody()->getContents());
462+
throw new ApiException('Json decode failed. Got response: ' . $responseBody);
462463
}
463464
if (array_key_exists('d', $json)) {
464465
if (array_key_exists('__next', $json['d'])) {
@@ -608,7 +609,8 @@ private function acquireAccessToken(): void
608609
$response = $this->client()->post($this->getTokenUrl(), $body);
609610

610611
Psr7\Message::rewindBody($response);
611-
$body = json_decode($response->getBody()->getContents(), true);
612+
$responseBody = $response->getBody()->getContents();
613+
$body = json_decode($responseBody, true);
612614

613615
if (json_last_error() === JSON_ERROR_NONE) {
614616
$this->accessToken = $body['access_token'];
@@ -619,7 +621,7 @@ private function acquireAccessToken(): void
619621
call_user_func($this->tokenUpdateCallback, $this);
620622
}
621623
} else {
622-
throw new ApiException('Could not acquire tokens, json decode failed. Got response: ' . $response->getBody()->getContents());
624+
throw new ApiException('Could not acquire tokens, json decode failed. Got response: ' . $responseBody);
623625
}
624626
} catch (BadResponseException $ex) {
625627
$this->parseExceptionForErrorMessages($ex);

tests/ConnectionTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use GuzzleHttp\HandlerStack;
1010
use GuzzleHttp\Psr7\Response;
1111
use PHPUnit\Framework\TestCase;
12+
use Picqer\Financials\Exact\ApiException;
1213
use Picqer\Financials\Exact\Connection;
1314

1415
class ConnectionTest extends TestCase
@@ -50,6 +51,44 @@ public function testGetIncludesDivisionInUrlForRegularEndpoint(): void
5051
$this->assertStringContainsString((string) $divisionNumber, $mockHandler->getLastRequest()->getUri()->__toString());
5152
}
5253

54+
public function testResponseIsLoggedIfResponseIsNotValidJson(): void
55+
{
56+
$divisionNumber = random_int(0, PHP_INT_MAX);
57+
$mockHandler = new MockHandler([
58+
new Response(200, [], 'invalid json'),
59+
]);
60+
$handlerStack = HandlerStack::create($mockHandler);
61+
$client = new Client(['handler' => $handlerStack]);
62+
$connection = new Connection();
63+
$connection->setClient($client);
64+
$connection->setDivision($divisionNumber);
65+
$connection->setAccessToken('1234567890');
66+
$connection->setTokenExpires(time() + 60);
67+
68+
$this->expectException(ApiException::class);
69+
$this->expectExceptionMessage('Json decode failed. Got response: invalid json');
70+
71+
$connection->get('crm/Accounts');
72+
}
73+
74+
public function testResponseIsLoggedIfAcquiringTokensFailed(): void
75+
{
76+
$divisionNumber = random_int(0, PHP_INT_MAX);
77+
$mockHandler = new MockHandler([
78+
new Response(200, [], 'invalid json'),
79+
]);
80+
$handlerStack = HandlerStack::create($mockHandler);
81+
$client = new Client(['handler' => $handlerStack]);
82+
$connection = new Connection();
83+
$connection->setClient($client);
84+
$connection->setDivision($divisionNumber);
85+
86+
$this->expectException(ApiException::class);
87+
$this->expectExceptionMessage('Could not acquire tokens, json decode failed. Got response: invalid json');
88+
89+
$connection->get('crm/Accounts');
90+
}
91+
5392
public function endpointsThatDontUseDivisionInUrl(): array
5493
{
5594
return [

0 commit comments

Comments
 (0)