From aede51752c45684d3b2a4765f570fb7a28feee99 Mon Sep 17 00:00:00 2001 From: cykolln <78871498+cykolln@users.noreply.github.com> Date: Tue, 23 Feb 2021 09:22:20 -0800 Subject: [PATCH] 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 --- CHANGELOG.md | 4 ++++ README.md | 15 +++++++++++++-- src/Exception/KlaviyoApiException.php | 5 +++++ src/Exception/KlaviyoAuthenticationException.php | 2 +- src/Exception/KlaviyoRateLimitException.php | 2 +- .../KlaviyoResourceNotFoundException.php | 2 +- src/KlaviyoAPI.php | 10 +++++----- 7 files changed, 30 insertions(+), 10 deletions(-) create mode 100644 src/Exception/KlaviyoApiException.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 0d40a48..b710236 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 565df5a..e4ab206 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/Exception/KlaviyoApiException.php b/src/Exception/KlaviyoApiException.php new file mode 100644 index 0000000..b8b8cbe --- /dev/null +++ b/src/Exception/KlaviyoApiException.php @@ -0,0 +1,5 @@ +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 ) {