Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stats: Return error for API requests #11520

Merged
merged 3 commits into from
Mar 7, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion modules/stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -1745,6 +1745,9 @@ function stats_get_from_restapi( $args = array(), $resource = '' ) {
$time = key( $stats_cache[ $cache_key ] );
if ( time() - $time < ( 5 * MINUTE_IN_SECONDS ) ) {
$cached_stats = $stats_cache[ $cache_key ][ $time ];
if ( is_wp_error( $cached_stats ) ) {
return $cached_stats;
}
$cached_stats = (object) array_merge( array( 'cached_at' => $time ), (array) $cached_stats );
return $cached_stats;
}
Expand All @@ -1754,7 +1757,7 @@ function stats_get_from_restapi( $args = array(), $resource = '' ) {
// Do the dirty work.
$response = Jetpack_Client::wpcom_json_api_request_as_blog( $endpoint, $api_version, $args );
if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
$data = array();
$data = $response;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$response does not have to be a WP_Error for every non-200 response.

I think we should do something like:

$data = is_wp_error( $response ) ? $response : new WP_Error( 'stats_error' );

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense. Before 7.1, we just returned the response, but there could be unexpected cases so we should force the error. 👍

#11582

} else {
$data = json_decode( wp_remote_retrieve_body( $response ) );
}
Expand Down