Skip to content
This repository was archived by the owner on Aug 18, 2023. It is now read-only.

Commit aede517

Browse files
authored
Updated handleResponse function to give more detailed error message (#41)
* Updated handleResponse function to give more detailed error message * Updated CHANGELOG * Created new exception and updated inheritance structure * updated from pr feedback * update to readme and changelog
1 parent 097f9ef commit aede517

7 files changed

Lines changed: 30 additions & 10 deletions

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## CHANGELOG
22

3+
### 2.2.5
4+
- Update - Add error details into KlaviyoAPI handleResponse function
5+
- Update - Add KlaviyoApiException
6+
37
### 2.2.4
48
- Fix - Instantiate KlaviyoRateLimitException properly
59
- Update - Add retryAfter as an array key for the KlaviyoRateLimitException message

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,18 @@ $client->profiles->getAllProfileMetricsTimeline( 'ProfileId' );
151151
$client->profiles->getProfileMetricTimeline( 'ProfileId', 'MetricId' );
152152
```
153153

154-
## Rate Limiting
155-
If a rate limit happens it will throw a Klaviyo/Exception/KlaviyoRateLimitException.
154+
## Exceptions
155+
156+
### Klaviyo\Exception\KlaviyoApiException
157+
Thrown when there is an issue making an API request. After you catch this exception, you can use getMessage() and it will return a string containing more details about the issue that occurred.
158+
159+
### Klaviyo\Exception\KlaviyoRateLimitException
160+
If a rate limit happens it will throw a Klaviyo\Exception\KlaviyoRateLimitException.
156161
After you catch this exception you can use getMessage() and it will return a JSON encoded array:
157162
`{"detail":"Request was throttled. Expected available in 26.0 seconds.","retryAfter":26}`
163+
164+
### Klaviyo\Exception\KlaviyoAuthenticationException
165+
Thrown when there is an authentication error when making an API request, usually caused by an invalid API key.
166+
167+
### Klaviyo\Exception\KlaviyoResourceNotFoundException
168+
Thrown when the system attempts to update a property that doesn't exist. For example, attempting to update a list that doesn't exist on the account.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
namespace Klaviyo\Exception;
4+
5+
class KlaviyoApiException extends KlaviyoException {}

src/Exception/KlaviyoAuthenticationException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
namespace Klaviyo\Exception;
44

5-
class KlaviyoAuthenticationException extends KlaviyoException {}
5+
class KlaviyoAuthenticationException extends KlaviyoApiException {}

src/Exception/KlaviyoRateLimitException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
namespace Klaviyo\Exception;
44

5-
class KlaviyoRateLimitException extends KlaviyoException {}
5+
class KlaviyoRateLimitException extends KlaviyoApiException {}

src/Exception/KlaviyoResourceNotFoundException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
namespace Klaviyo\Exception;
44

5-
class KlaviyoResourceNotFoundException extends KlaviyoException {}
5+
class KlaviyoResourceNotFoundException extends KlaviyoApiException {}

src/KlaviyoAPI.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Klaviyo\Exception\KlaviyoException;
77
use Klaviyo\Exception\KlaviyoRateLimitException;
88
use Klaviyo\Exception\KlaviyoResourceNotFoundException;
9+
use Klaviyo\Exception\KlaviyoApiException;
910
use Klaviyo\Model\ProfileModel;
1011

1112
abstract class KlaviyoAPI
@@ -31,7 +32,6 @@ abstract class KlaviyoAPI
3132
*/
3233
const ERROR_INVALID_API_KEY = 'Invalid API Key.';
3334
const ERROR_RESOURCE_DOES_NOT_EXIST = 'The requested resource does not exist.';
34-
const ERROR_NON_200_STATUS = 'Request Failed with HTTP Status Code: %s';
3535

3636
/**
3737
* Request options
@@ -175,15 +175,15 @@ private function request( $method, $path, $options, $isPublic = false, $isV1 = f
175175
private function handleResponse( $response, $statusCode, $isPublic )
176176
{
177177
if ( $statusCode == 403 ) {
178-
throw new KlaviyoAuthenticationException(self::ERROR_INVALID_API_KEY);
178+
throw new KlaviyoAuthenticationException(self::ERROR_INVALID_API_KEY, $statusCode);
179179
} else if ( $statusCode == 404 ) {
180-
throw new KlaviyoResourceNotFoundException(self::ERROR_RESOURCE_DOES_NOT_EXIST);
180+
throw new KlaviyoResourceNotFoundException( self::ERROR_RESOURCE_DOES_NOT_EXIST, $statusCode);
181181
} else if ( $statusCode == 429 ) {
182182
throw new KlaviyoRateLimitException(
183-
$this->returnRateLimit( $this->decodeJsonResponse( $response ) )
183+
$this->returnRateLimit( $this->decodeJsonResponse( $response ), $statusCode )
184184
);
185185
} else if ( $statusCode != 200 ) {
186-
throw new KlaviyoException( sprintf( self::ERROR_NON_200_STATUS, $statusCode ) );
186+
throw new KlaviyoApiException($this->decodeJsonResponse( $response )['detail'], $statusCode);
187187
}
188188

189189
if ( $isPublic ) {

0 commit comments

Comments
 (0)