From 73e1f2c19473c0848aeef642e29756d93e2753d2 Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Tue, 24 Sep 2024 12:28:30 +0800 Subject: [PATCH] Check response in `get_error_message_string` is an array --- src/class-convertkit-api-v4.php | 9 +++++++-- tests/wpunit/APITest.php | 9 +-------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/class-convertkit-api-v4.php b/src/class-convertkit-api-v4.php index ba13158..e755610 100644 --- a/src/class-convertkit-api-v4.php +++ b/src/class-convertkit-api-v4.php @@ -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 = ''; diff --git a/tests/wpunit/APITest.php b/tests/wpunit/APITest.php index ec894ec..6746d60 100644 --- a/tests/wpunit/APITest.php +++ b/tests/wpunit/APITest.php @@ -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);