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

Commit

Permalink
Updated handleResponse function to give more detailed error message (#41
Browse files Browse the repository at this point in the history
)

* 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
  • Loading branch information
cykolln authored Feb 23, 2021
1 parent 097f9ef commit aede517
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## CHANGELOG

### 2.2.5
- Update - Add error details into KlaviyoAPI handleResponse function
- Update - Add KlaviyoApiException

### 2.2.4
- Fix - Instantiate KlaviyoRateLimitException properly
- Update - Add retryAfter as an array key for the KlaviyoRateLimitException message
Expand Down
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,18 @@ $client->profiles->getAllProfileMetricsTimeline( 'ProfileId' );
$client->profiles->getProfileMetricTimeline( 'ProfileId', 'MetricId' );
```

## Rate Limiting
If a rate limit happens it will throw a Klaviyo/Exception/KlaviyoRateLimitException.
## Exceptions

### Klaviyo\Exception\KlaviyoApiException
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.

### Klaviyo\Exception\KlaviyoRateLimitException
If a rate limit happens it will throw a Klaviyo\Exception\KlaviyoRateLimitException.
After you catch this exception you can use getMessage() and it will return a JSON encoded array:
`{"detail":"Request was throttled. Expected available in 26.0 seconds.","retryAfter":26}`

### Klaviyo\Exception\KlaviyoAuthenticationException
Thrown when there is an authentication error when making an API request, usually caused by an invalid API key.

### Klaviyo\Exception\KlaviyoResourceNotFoundException
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.
5 changes: 5 additions & 0 deletions src/Exception/KlaviyoApiException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace Klaviyo\Exception;

class KlaviyoApiException extends KlaviyoException {}
2 changes: 1 addition & 1 deletion src/Exception/KlaviyoAuthenticationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

namespace Klaviyo\Exception;

class KlaviyoAuthenticationException extends KlaviyoException {}
class KlaviyoAuthenticationException extends KlaviyoApiException {}
2 changes: 1 addition & 1 deletion src/Exception/KlaviyoRateLimitException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

namespace Klaviyo\Exception;

class KlaviyoRateLimitException extends KlaviyoException {}
class KlaviyoRateLimitException extends KlaviyoApiException {}
2 changes: 1 addition & 1 deletion src/Exception/KlaviyoResourceNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

namespace Klaviyo\Exception;

class KlaviyoResourceNotFoundException extends KlaviyoException {}
class KlaviyoResourceNotFoundException extends KlaviyoApiException {}
10 changes: 5 additions & 5 deletions src/KlaviyoAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Klaviyo\Exception\KlaviyoException;
use Klaviyo\Exception\KlaviyoRateLimitException;
use Klaviyo\Exception\KlaviyoResourceNotFoundException;
use Klaviyo\Exception\KlaviyoApiException;
use Klaviyo\Model\ProfileModel;

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

/**
* Request options
Expand Down Expand Up @@ -175,15 +175,15 @@ private function request( $method, $path, $options, $isPublic = false, $isV1 = f
private function handleResponse( $response, $statusCode, $isPublic )
{
if ( $statusCode == 403 ) {
throw new KlaviyoAuthenticationException(self::ERROR_INVALID_API_KEY);
throw new KlaviyoAuthenticationException(self::ERROR_INVALID_API_KEY, $statusCode);
} else if ( $statusCode == 404 ) {
throw new KlaviyoResourceNotFoundException(self::ERROR_RESOURCE_DOES_NOT_EXIST);
throw new KlaviyoResourceNotFoundException( self::ERROR_RESOURCE_DOES_NOT_EXIST, $statusCode);
} else if ( $statusCode == 429 ) {
throw new KlaviyoRateLimitException(
$this->returnRateLimit( $this->decodeJsonResponse( $response ) )
$this->returnRateLimit( $this->decodeJsonResponse( $response ), $statusCode )
);
} else if ( $statusCode != 200 ) {
throw new KlaviyoException( sprintf( self::ERROR_NON_200_STATUS, $statusCode ) );
throw new KlaviyoApiException($this->decodeJsonResponse( $response )['detail'], $statusCode);
}

if ( $isPublic ) {
Expand Down

0 comments on commit aede517

Please sign in to comment.