Skip to content

Commit 0148698

Browse files
committed
Throw Api\Exception when client returns a non 200 response
1 parent 04f4b89 commit 0148698

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

src/Collection.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,12 @@ public function next()
156156
$indexResponse = $this->client->index($this->resource, $this->filters);
157157

158158
$httpCode = $indexResponse->getHttpCode();
159-
Util::ensure(200, $httpCode, "Did not receive 200 from API. Instead received {$httpCode}");
159+
Util::ensure(
160+
200,
161+
$httpCode,
162+
Exception::class,
163+
["Did not receive 200 from API. Instead received {$httpCode}", $indexResponse]
164+
);
160165

161166
$response = $indexResponse->getResponse();
162167
$this->limit = $response['pagination']['limit'];

tests/CollectionTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,44 @@ public function selectMissingKeys()
300300
);
301301
}
302302

303+
/**
304+
* @test
305+
* @covers ::next
306+
*/
307+
public function collectionThrowsApiExceptionWith400Response()
308+
{
309+
$response = new Response(400, ['Content-Type' => ['application/json']], []);
310+
$mockClient = $this->getMockBuilder(ClientInterface::class)->getMock();
311+
$mockClient->method('index')->willReturn($response);
312+
$collection = new Collection($mockClient, 'basic', []);
313+
try {
314+
$collection->next();
315+
$this->fail('No Exception throw');
316+
} catch (Exception $e) {
317+
$this->assertSame('Did not receive 200 from API. Instead received 400', $e->getMessage());
318+
$this->assertSame($response, $e->getResponse());
319+
}
320+
}
321+
322+
/**
323+
* @test
324+
* @covers ::next
325+
*/
326+
public function collectionThrowsApiExceptionWith500Response()
327+
{
328+
$response = new Response(500, ['Content-Type' => ['application/json']], []);
329+
$mockClient = $this->getMockBuilder(ClientInterface::class)->getMock();
330+
$mockClient->method('index')->willReturn($response);
331+
$collection = new Collection($mockClient, 'basic', []);
332+
try {
333+
$collection->next();
334+
$this->fail('No Exception throw');
335+
} catch (Exception $e) {
336+
$this->assertSame('Did not receive 200 from API. Instead received 500', $e->getMessage());
337+
$this->assertSame($response, $e->getResponse());
338+
}
339+
}
340+
303341
private function getClient(array $items = self::DEFAULT_RESULT_SET) : ClientInterface
304342
{
305343
$callback = function (string $resource, array $filters) use ($items) {

0 commit comments

Comments
 (0)