|
3 | 3 | namespace TraderInteractive\Api; |
4 | 4 |
|
5 | 5 | use ArrayObject; |
| 6 | +use GuzzleHttp\Psr7\Utils; |
6 | 7 | use TraderInteractive\Util; |
7 | 8 | use GuzzleHttp\Client as GuzzleClient; |
8 | 9 | use GuzzleHttp\ClientInterface as GuzzleClientInterface; |
@@ -74,17 +75,25 @@ public function end(string $endHandle) : ResponseInterface |
74 | 75 | foreach ($results as $handle => $response) { |
75 | 76 | try { |
76 | 77 | $contents = (string)$response->getBody(); |
77 | | - if (trim($contents) !== '') { |
78 | | - json_decode($contents, true); |
79 | | - Util::ensure( |
80 | | - JSON_ERROR_NONE, |
81 | | - json_last_error(), |
82 | | - '\UnexpectedValueException', |
83 | | - [json_last_error_msg()] |
84 | | - ); |
| 78 | + if (trim($contents) === '') { |
| 79 | + $this->responses[$handle] = $response; |
| 80 | + continue; |
85 | 81 | } |
86 | 82 |
|
87 | | - $this->responses[$handle] = $response; |
| 83 | + json_decode($contents, true); |
| 84 | + if (json_last_error() === JSON_ERROR_NONE) { |
| 85 | + $this->responses[$handle] = $response; |
| 86 | + continue; |
| 87 | + } |
| 88 | + |
| 89 | + $errorBody = [ |
| 90 | + 'error' => [ |
| 91 | + 'message' => 'Original API response did not contain valid JSON: ' . json_last_error_msg(), |
| 92 | + 'originalResponse' => $contents, |
| 93 | + ], |
| 94 | + ]; |
| 95 | + |
| 96 | + $this->responses[$handle] = $response->withBody(Utils::streamFor(json_encode($errorBody))); |
88 | 97 | } catch (\Exception $e) { |
89 | 98 | $this->exceptions[$handle] = $e; |
90 | 99 | } |
|
0 commit comments