mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 18:16:38 +08:00
🔊 Add logging to the internal REST service
This commit is contained in:
parent
57ee70acba
commit
ffbe9c08d5
2 changed files with 44 additions and 1 deletions
|
@ -318,7 +318,9 @@ return array(
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
'settings.service.rest-service' => static function ( ContainerInterface $container ) : InternalRestService {
|
'settings.service.rest-service' => static function ( ContainerInterface $container ) : InternalRestService {
|
||||||
return new InternalRestService();
|
return new InternalRestService(
|
||||||
|
$container->get( 'woocommerce.logger.woocommerce' )
|
||||||
|
);
|
||||||
},
|
},
|
||||||
'settings.service.sanitizer' => static function ( ContainerInterface $container ) : DataSanitizer {
|
'settings.service.sanitizer' => static function ( ContainerInterface $container ) : DataSanitizer {
|
||||||
return new DataSanitizer();
|
return new DataSanitizer();
|
||||||
|
|
|
@ -10,15 +10,42 @@ declare( strict_types = 1 );
|
||||||
namespace WooCommerce\PayPalCommerce\Settings\Service;
|
namespace WooCommerce\PayPalCommerce\Settings\Service;
|
||||||
|
|
||||||
use Throwable;
|
use Throwable;
|
||||||
|
use Psr\Log\LoggerInterface;
|
||||||
|
|
||||||
class InternalRestService {
|
class InternalRestService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Logger instance.
|
||||||
|
*
|
||||||
|
* In this case, the logger is quite important for debugging, because the main
|
||||||
|
* functionality of this class cannot be step-debugged using Xdebug: While
|
||||||
|
* a Xdebug session is active, the remote call to the current server is also
|
||||||
|
* blocked and will end in a timeout.
|
||||||
|
*
|
||||||
|
* @var LoggerInterface
|
||||||
|
*/
|
||||||
|
private LoggerInterface $logger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor.
|
||||||
|
*
|
||||||
|
* @param LoggerInterface $logger Logger instance.
|
||||||
|
*/
|
||||||
|
public function __construct( LoggerInterface $logger ) {
|
||||||
|
$this->logger = $logger;
|
||||||
|
}
|
||||||
|
|
||||||
public function get_data( string $endpoint ) : array {
|
public function get_data( string $endpoint ) : array {
|
||||||
$token = $this->generate_token( $endpoint );
|
$token = $this->generate_token( $endpoint );
|
||||||
$rest_url = rest_url( $endpoint );
|
$rest_url = rest_url( $endpoint );
|
||||||
|
|
||||||
$response = wp_remote_get(
|
$response = wp_remote_get(
|
||||||
|
$this->logger->info( "Calling internal REST endpoint: $rest_url" );
|
||||||
|
|
||||||
|
$response = wp_remote_request(
|
||||||
$rest_url,
|
$rest_url,
|
||||||
array(
|
array(
|
||||||
|
'method' => 'GET',
|
||||||
'headers' => array(
|
'headers' => array(
|
||||||
'X-Internal-Token' => $token,
|
'X-Internal-Token' => $token,
|
||||||
'Content-Type' => 'application/json',
|
'Content-Type' => 'application/json',
|
||||||
|
@ -27,6 +54,8 @@ class InternalRestService {
|
||||||
);
|
);
|
||||||
|
|
||||||
if ( is_wp_error( $response ) ) {
|
if ( is_wp_error( $response ) ) {
|
||||||
|
$this->logger->error( 'Internal REST error', array( 'response' => $response ) );
|
||||||
|
|
||||||
return array();
|
return array();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,13 +64,25 @@ class InternalRestService {
|
||||||
try {
|
try {
|
||||||
$json = json_decode( $body, true, 512, JSON_THROW_ON_ERROR );
|
$json = json_decode( $body, true, 512, JSON_THROW_ON_ERROR );
|
||||||
} catch ( Throwable $exception ) {
|
} catch ( Throwable $exception ) {
|
||||||
|
$this->logger->error(
|
||||||
|
'Internal REST error: Invalid JSON response',
|
||||||
|
array(
|
||||||
|
'error' => $exception->getMessage(),
|
||||||
|
'response_body' => $body,
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
return array();
|
return array();
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( ! $json || empty( $json['success'] ) ) {
|
if ( ! $json || empty( $json['success'] ) ) {
|
||||||
|
$this->logger->error( 'Internal REST error: Invalid response', array( 'json' => $json ) );
|
||||||
|
|
||||||
return array();
|
return array();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->logger->info( 'Internal REST success', array( 'data' => $json['data'] ) );
|
||||||
|
|
||||||
return $json['data'];
|
return $json['data'];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue