Skip to content

Commit

Permalink
Merge pull request #14 from ConvertKit/api-error-data
Browse files Browse the repository at this point in the history
API: Include HTTP Response Code in `WP_Error`
  • Loading branch information
n7studios authored Feb 10, 2023
2 parents 1edbe6c + af304a6 commit b845565
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
30 changes: 25 additions & 5 deletions src/class-convertkit-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -1719,7 +1719,11 @@ private function request( $endpoint, $method = 'get', $params = array(), $retry_
case 429:
// If retry on rate limit hit is disabled, return a WP_Error.
if ( ! $retry_if_rate_limit_hit ) {
return new WP_Error( 'convertkit_api_error', $this->get_error_message( 'request_rate_limit_exceeded' ) );
return new WP_Error(
'convertkit_api_error',
$this->get_error_message( 'request_rate_limit_exceeded' ),
$http_response_code
);
}

// Retry the request a final time, waiting 2 seconds before.
Expand All @@ -1728,23 +1732,39 @@ private function request( $endpoint, $method = 'get', $params = array(), $retry_

// Internal server error.
case 500:
return new WP_Error( 'convertkit_api_error', $this->get_error_message( 'request_internal_server_error' ) );
return new WP_Error(
'convertkit_api_error',
$this->get_error_message( 'request_internal_server_error' ),
$http_response_code
);

// Bad gateway.
case 502:
return new WP_Error( 'convertkit_api_error', $this->get_error_message( 'request_bad_gateway' ) );
return new WP_Error(
'convertkit_api_error',
$this->get_error_message( 'request_bad_gateway' ),
$http_response_code
);
}

// If the response is null, json_decode() failed as the body could not be decoded.
if ( is_null( $response ) ) {
$this->log( 'API: Error: ' . sprintf( $this->get_error_message( 'response_type_unexpected' ), $body ) );
return new WP_Error( 'convertkit_api_error', sprintf( $this->get_error_message( 'response_type_unexpected' ), $body ) );
return new WP_Error(
'convertkit_api_error',
sprintf( $this->get_error_message( 'response_type_unexpected' ), $body ),
$http_response_code
);
}

// If an error message or code exists in the response, return a WP_Error.
if ( isset( $response['error'] ) ) {
$this->log( 'API: Error: ' . $response['error'] . ': ' . $response['message'] );
return new WP_Error( 'convertkit_api_error', $response['error'] . ': ' . $response['message'] );
return new WP_Error(
'convertkit_api_error',
$response['error'] . ': ' . $response['message'],
$http_response_code
);
}

return $response;
Expand Down
18 changes: 18 additions & 0 deletions tests/wpunit/APITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,21 @@ public function tearDown(): void
parent::tearDown();
}

/**
* Test that a 401 unauthorized error gracefully returns a WP_Error.
*
* @since 1.3.2
*/
public function test401Unauthorized()
{
$api = new ConvertKit_API('fakeApiKey', 'fakeApiSecret');
$result = $api->account();
$this->assertInstanceOf(WP_Error::class, $result);
$this->assertEquals($result->get_error_code(), $this->errorCode);
$this->assertEquals($result->get_error_message(), 'Authorization Failed: API Key not valid');
$this->assertEquals($result->get_error_data($result->get_error_code()), 401);
}

/**
* Test that a 429 internal server error gracefully returns a WP_Error.
*
Expand All @@ -75,6 +90,7 @@ public function test429RateLimitHit()
$this->assertInstanceOf(WP_Error::class, $result);
$this->assertEquals($result->get_error_code(), $this->errorCode);
$this->assertEquals($result->get_error_message(), 'ConvertKit API Error: Rate limit hit.');
$this->assertEquals($result->get_error_data($result->get_error_code()), 429);
}

/**
Expand All @@ -90,6 +106,7 @@ public function test500InternalServerError()
$this->assertInstanceOf(WP_Error::class, $result);
$this->assertEquals($result->get_error_code(), $this->errorCode);
$this->assertEquals($result->get_error_message(), 'ConvertKit API Error: Internal server error.');
$this->assertEquals($result->get_error_data($result->get_error_code()), 500);
}

/**
Expand All @@ -105,6 +122,7 @@ public function test502BadGateway()
$this->assertInstanceOf(WP_Error::class, $result);
$this->assertEquals($result->get_error_code(), $this->errorCode);
$this->assertEquals($result->get_error_message(), 'ConvertKit API Error: Bad gateway.');
$this->assertEquals($result->get_error_data($result->get_error_code()), 502);
}

/**
Expand Down

0 comments on commit b845565

Please sign in to comment.