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

Commit 85618ca

Browse files
authored
Merge pull request #58 from bradleymellen/master
restore old generic exception message for non-200 responses that don't provide any details
2 parents e1737f6 + 20ff592 commit 85618ca

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

src/KlaviyoAPI.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ abstract class KlaviyoAPI
3232
*/
3333
const ERROR_INVALID_API_KEY = 'Invalid API Key.';
3434
const ERROR_RESOURCE_DOES_NOT_EXIST = 'The requested resource does not exist.';
35+
const ERROR_NON_200_STATUS = 'Request Failed with HTTP Status Code: %s';
3536

3637
/**
3738
* Request options
@@ -175,21 +176,22 @@ private function request( $method, $path, $options, $isPublic = false, $isV1 = f
175176
*/
176177
private function handleResponse( $response, $statusCode, $isPublic )
177178
{
179+
$decoded_response = $this->decodeJsonResponse($response);
178180
if ( $statusCode == 403 ) {
179181
throw new KlaviyoAuthenticationException(self::ERROR_INVALID_API_KEY, $statusCode);
180182
} else if ( $statusCode == 429 ) {
181183
throw new KlaviyoRateLimitException(
182-
$this->returnRateLimit( $this->decodeJsonResponse( $response ) )
184+
$this->returnRateLimit( $decoded_response )
183185
);
184186
} else if ($statusCode < 200 || $statusCode >= 300) {
185-
throw new KlaviyoApiException($this->decodeJsonResponse( $response )['detail'], $statusCode);
187+
throw new KlaviyoApiException(isset($decoded_response['detail']) ? $decoded_response['detail'] : sprintf(self::ERROR_NON_200_STATUS, $statusCode), $statusCode);
186188
}
187189

188190
if ( $isPublic ) {
189191
return $response;
190192
}
191193

192-
return $this->decodeJsonResponse( $response );
194+
return $decoded_response;
193195
}
194196

195197
/**

0 commit comments

Comments
 (0)