Skip to content

Commit

Permalink
Merge pull request #80 from ConvertKit/fix-no-body-response
Browse files Browse the repository at this point in the history
Check response in `get_error_message_string` is an array
  • Loading branch information
n7studios authored Sep 25, 2024
2 parents 389a483 + 73e1f2c commit 4ed8a5e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
9 changes: 7 additions & 2 deletions src/class-convertkit-api-v4.php
Original file line number Diff line number Diff line change
Expand Up @@ -1540,11 +1540,16 @@ private function is_production_site() {
*
* @since 2.0.0
*
* @param array $response API Response.
* @return string Error Message(s).
* @param null|array $response API Response.
* @return string Error Message(s).
*/
private function get_error_message_string( $response ) {

// If there is no API response body (such as a 429 error), there'll be no error message to return.
if ( ! is_array( $response ) ) {
return '';
}

// Most API responses contain the `errors` key.
if ( array_key_exists( 'errors', $response ) ) {
$error_message = '';
Expand Down
9 changes: 1 addition & 8 deletions tests/wpunit/APITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,7 @@ public function test429RateLimitHit()
// Force WordPress HTTP classes and functions to return a 429 error.
$this->mockResponses(
429,
'Rate limit hit',
wp_json_encode(
array(
'errors' => array(
'Rate limit hit.',
),
)
)
'Rate limit hit'
);
$result = $this->api->get_account(); // The API function we use doesn't matter, as mockResponse forces a 429 error.
$this->assertInstanceOf(WP_Error::class, $result);
Expand Down

0 comments on commit 4ed8a5e

Please sign in to comment.