Skip to content

Commit

Permalink
add logs
Browse files Browse the repository at this point in the history
  • Loading branch information
MathieuLamiot committed Jun 28, 2024
1 parent 0ad200f commit 9ce45e1
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace WP_Rocket\Engine\Common\JobManager\APIHandler;

use WP_Rocket\Logger\LoggerAware;
use WP_Rocket\Logger\LoggerAwareInterface;
use WP_Error;

/**
Expand All @@ -12,7 +14,9 @@
*
* @package WP_Rocket\Engine\Common\JobManager\APIHandler
*/
abstract class AbstractSafeAPIClient {
abstract class AbstractSafeAPIClient implements LoggerAwareInterface {

use LoggerAware;

/**
* Get the transient key.
Expand Down Expand Up @@ -63,7 +67,14 @@ public function send_post_request( $params = [], $safe = false ) {
private function send_request( $method, $params = [], $safe = false ) {
$api_url = $this->get_api_url();

if ( get_transient( $this->get_transient_key() . '_timeout_active' ) ) {
$timeout_transient_name = $this->get_transient_key() . '_timeout_active';
if ( get_transient( $timeout_transient_name ) ) {
$this->logger::debug(
"Request to {$api_url} avoided: {$timeout_transient_name} is set.",
[
'method' => __METHOD__,
]
);
return new WP_Error( 429, __( 'Too many requests.', 'rocket' ) );
}

Expand All @@ -79,6 +90,12 @@ private function send_request( $method, $params = [], $safe = false ) {
$body = wp_remote_retrieve_body( $response );
if ( empty( $body ) || ( is_array( $response ) && ! empty( $response['response']['code'] ) && 200 !== $response['response']['code'] ) ) {
$this->set_timeout_transients();
$this->logger::debug(
"Request to {$api_url} failed: {$this->get_transient_key()}'_timeout_active is set.",
[
'method' => __METHOD__,
]
);
return new WP_Error( 500, __( 'Not valid response.', 'rocket' ) );
}

Expand Down Expand Up @@ -143,7 +160,12 @@ private function send_remote_request( $api_url, $method, $params, $safe ) {
case 'POST':
return wp_safe_remote_post( $api_url, $params );
}

$this->logger::debug(
"Request to {$api_url} failed: {$method} is not a supported method.",
[
'method' => __METHOD__,
]
);
return new WP_Error( 400, __( 'Not valid request type.', 'rocket' ) );
}
}

0 comments on commit 9ce45e1

Please sign in to comment.