🥅 Additional error handling during merchant login

This commit is contained in:
Philipp Stracker 2025-03-07 12:43:56 +01:00
parent 1c608ac0c2
commit 21147ae803
No known key found for this signature in database
3 changed files with 39 additions and 16 deletions

View file

@ -9,11 +9,11 @@ declare( strict_types = 1 );
namespace WooCommerce\PayPalCommerce\Settings\Endpoint;
use Exception;
use WP_REST_Server;
use WP_REST_Response;
use WP_REST_Request;
use WooCommerce\PayPalCommerce\Settings\Data\GeneralSettings;
use WooCommerce\PayPalCommerce\Settings\Service\InternalRestService;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PartnersEndpoint;
/**
@ -257,9 +257,17 @@ class CommonRestEndpoint extends RestEndpoint {
* @return WP_REST_Response Seller details, provided by PayPal's API.
*/
public function get_seller_account_info() : WP_REST_Response {
$seller_status = $this->partners_endpoint->seller_status();
try {
$seller_status = $this->partners_endpoint->seller_status();
return $this->return_success( array( 'country' => $seller_status->country() ) );
$seller_data = array(
'country' => $seller_status->country(),
);
return $this->return_success( $seller_data );
} catch ( Exception $ex ) {
return $this->return_error( $ex->getMessage() );
}
}
/**

View file

@ -446,9 +446,24 @@ class AuthenticationManager {
try {
$endpoint = CommonRestEndpoint::seller_account_route( true );
$details = $this->rest_service->get_response( $endpoint );
$response = $this->rest_service->get_response( $endpoint );
if ( ! $response['success'] ) {
$this->logger->warning( 'Failed to load merchant details!', $response );
return;
}
$details = $response['data'];
} catch ( Throwable $exception ) {
$this->logger->warning( 'Could not determine merchant country: ' . $exception->getMessage() );
return;
}
if ( ! isset( $details['country'] ) ) {
$this->logger->warning( 'Missing country in merchant details' );
return;
}

View file

@ -10,6 +10,7 @@ declare( strict_types = 1 );
namespace WooCommerce\PayPalCommerce\Settings\Service;
use Throwable;
use RuntimeException;
use Psr\Log\LoggerInterface;
use WP_Http_Cookie;
@ -48,6 +49,7 @@ class InternalRestService {
*
* @param string $endpoint The endpoint for which the token is generated.
* @return mixed The REST response.
* @throws RuntimeException In case the remote request fails, an exception is thrown.
*/
public function get_response( string $endpoint ) {
$rest_url = rest_url( $endpoint );
@ -69,9 +71,11 @@ class InternalRestService {
);
if ( is_wp_error( $response ) ) {
$this->logger->error( 'Internal REST error', array( 'response' => $response ) );
// Error: The wp_remote_request() call failed (timeout or similar).
$error = new RuntimeException( 'Internal REST error' );
$this->logger->error( $error->getMessage(), array( 'response' => $response ) );
return array();
throw $error;
}
$body = wp_remote_retrieve_body( $response );
@ -79,26 +83,22 @@ class InternalRestService {
try {
$json = json_decode( $body, true, 512, JSON_THROW_ON_ERROR );
} catch ( Throwable $exception ) {
// Error: The returned body-string is not valid JSON.
$error = new RuntimeException( 'Internal REST error: Invalid JSON response' );
$this->logger->error(
'Internal REST error: Invalid JSON response',
$error->getMessage(),
array(
'error' => $exception->getMessage(),
'response_body' => $body,
)
);
return array();
throw $error;
}
if ( ! $json || empty( $json['success'] ) ) {
$this->logger->error( 'Internal REST error: Invalid response', array( 'json' => $json ) );
$this->logger->info( 'Internal REST success!', array( 'json' => $json ) );
return array();
}
$this->logger->info( 'Internal REST success', array( 'data' => $json['data'] ) );
return $json['data'];
return $json;
}
/**