From 67cbb3d718ba7586a3a3bd0270fc77abb5f4dece Mon Sep 17 00:00:00 2001 From: "Erik Trapin (ecoologic)" Date: Thu, 9 Nov 2023 13:54:38 +1000 Subject: [PATCH] Iterator handling errors --- README.md | 16 ++++++++++ .../Traits/Utility/PaginationIteratorTest.php | 32 ++++++++++++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3163b838..e75cea4c 100755 --- a/README.md +++ b/README.md @@ -176,6 +176,22 @@ This can be useful for filter endpoints like [active automations](https://develo $iterator = $client->automations()->iterator($params, 'findActive'); ``` +##### Catching API errors + +This doesn't change too much: + +```php +try { + foreach ($iterator as $ticket) { + // your code + } +} catch (ApiResponseException $e) { + $errorMessage = $e->getMessage(); +} +``` + +If you need to know at what point you got the error, you can store the required information inside the loop in your code. + #### FindAll using CBP (fine) If you still want use `findAll()`, until CBP becomes the default API response, you must explicitly request CBP responses by using the param `page[size]`. diff --git a/tests/Zendesk/API/UnitTests/Traits/Utility/PaginationIteratorTest.php b/tests/Zendesk/API/UnitTests/Traits/Utility/PaginationIteratorTest.php index a4b6cc28..5fed9b21 100644 --- a/tests/Zendesk/API/UnitTests/Traits/Utility/PaginationIteratorTest.php +++ b/tests/Zendesk/API/UnitTests/Traits/Utility/PaginationIteratorTest.php @@ -2,6 +2,10 @@ namespace Zendesk\API\UnitTests\Core; +use GuzzleHttp\Exception\RequestException; +use GuzzleHttp\Psr7\Request; +use GuzzleHttp\Psr7\Response; +use Zendesk\API\Exceptions\ApiResponseException; use Zendesk\API\Traits\Utility\Pagination\CbpStrategy; use Zendesk\API\Traits\Utility\Pagination\SinglePageStrategy; use Zendesk\API\UnitTests\BasicTest; @@ -13,16 +17,24 @@ class MockResource { private $resources; private $resourceName; private $callCount = 0; + private $errorMessage; - public function __construct($resourceName, $resources) + public function __construct($resourceName, $resources, $errorMessage = null) { $this->resourceName = $resourceName; $this->resources = $resources; $this->callCount = 0; + $this->errorMessage = $errorMessage; } public function findAll($params) { + if ($this->errorMessage) { + $request = new Request('GET', 'http://example.zendesk.com'); + $response = new Response(400, [], '{ "a": "json"}'); + $requestException = new RequestException($this->errorMessage, $request, $response); + throw new ApiResponseException($requestException); + } // Simulate two pages of resources $resources = $this->callCount === 0 ? $this->resources[0] @@ -158,4 +170,22 @@ public function testCustomMethod() $this->assertEquals(true, $mockResults->foundDifferent); $this->assertEquals($userParams, $mockResults->params); } + + public function testHandlesError() + { + $expectedErrorMessage = "BOOM!"; + $resultsKey = 'results'; + $userParams = []; + $mockResults = new MockResource($resultsKey, [], $expectedErrorMessage); + $strategy = new CbpStrategy($resultsKey, $userParams); + $iterator = new PaginationIterator($mockResults, $strategy); + + try { + iterator_to_array($iterator); + } catch (ApiResponseException $e) { + $actualErrorMessage = $e->getMessage(); + } + + $this->assertEquals($expectedErrorMessage, $actualErrorMessage); + } }