Log endpoint errors

This commit is contained in:
Alex P 2021-09-23 17:19:46 +03:00
parent efb5ab3d14
commit 7ef5991cb8
9 changed files with 108 additions and 30 deletions

View file

@ -10,6 +10,7 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Button\Endpoint; namespace WooCommerce\PayPalCommerce\Button\Endpoint;
use Exception;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Entity\OrderStatus; use WooCommerce\PayPalCommerce\ApiClient\Entity\OrderStatus;
@ -195,7 +196,9 @@ class ApproveOrderEndpoint implements EndpointInterface {
$this->session_handler->replace_order( $order ); $this->session_handler->replace_order( $order );
wp_send_json_success( $order ); wp_send_json_success( $order );
return true; return true;
} catch ( \RuntimeException $error ) { } catch ( Exception $error ) {
$this->logger->error( 'Order approve failed: ' . $error->getMessage() );
wp_send_json_error( wp_send_json_error(
array( array(
'name' => is_a( $error, PayPalApiException::class ) ? $error->name() : '', 'name' => is_a( $error, PayPalApiException::class ) ? $error->name() : '',

View file

@ -9,6 +9,8 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Button\Endpoint; namespace WooCommerce\PayPalCommerce\Button\Endpoint;
use Exception;
use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PurchaseUnit; use WooCommerce\PayPalCommerce\ApiClient\Entity\PurchaseUnit;
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException; use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
use WooCommerce\PayPalCommerce\ApiClient\Repository\CartRepository; use WooCommerce\PayPalCommerce\ApiClient\Repository\CartRepository;
@ -57,21 +59,30 @@ class ChangeCartEndpoint implements EndpointInterface {
*/ */
private $product_data_store; private $product_data_store;
/**
* The logger.
*
* @var LoggerInterface
*/
protected $logger;
/** /**
* ChangeCartEndpoint constructor. * ChangeCartEndpoint constructor.
* *
* @param \WC_Cart $cart The current WC cart object. * @param \WC_Cart $cart The current WC cart object.
* @param \WC_Shipping $shipping The current WC shipping object. * @param \WC_Shipping $shipping The current WC shipping object.
* @param RequestData $request_data The request data helper. * @param RequestData $request_data The request data helper.
* @param CartRepository $repository The repository for the current purchase items. * @param CartRepository $repository The repository for the current purchase items.
* @param \WC_Data_Store $product_data_store The data store for products. * @param \WC_Data_Store $product_data_store The data store for products.
* @param LoggerInterface $logger The logger.
*/ */
public function __construct( public function __construct(
\WC_Cart $cart, \WC_Cart $cart,
\WC_Shipping $shipping, \WC_Shipping $shipping,
RequestData $request_data, RequestData $request_data,
CartRepository $repository, CartRepository $repository,
\WC_Data_Store $product_data_store \WC_Data_Store $product_data_store,
LoggerInterface $logger
) { ) {
$this->cart = $cart; $this->cart = $cart;
@ -79,6 +90,7 @@ class ChangeCartEndpoint implements EndpointInterface {
$this->request_data = $request_data; $this->request_data = $request_data;
$this->repository = $repository; $this->repository = $repository;
$this->product_data_store = $product_data_store; $this->product_data_store = $product_data_store;
$this->logger = $logger;
} }
/** /**
@ -94,12 +106,13 @@ class ChangeCartEndpoint implements EndpointInterface {
* Handles the request. * Handles the request.
* *
* @return bool * @return bool
* @throws \Exception On error.
*/ */
public function handle_request(): bool { public function handle_request(): bool {
try { try {
return $this->handle_data(); return $this->handle_data();
} catch ( RuntimeException $error ) { } catch ( Exception $error ) {
$this->logger->error( 'Cart updating failed: ' . $error->getMessage() );
wp_send_json_error( wp_send_json_error(
array( array(
'name' => is_a( $error, PayPalApiException::class ) ? $error->name() : '', 'name' => is_a( $error, PayPalApiException::class ) ? $error->name() : '',
@ -116,7 +129,7 @@ class ChangeCartEndpoint implements EndpointInterface {
* Handles the request data. * Handles the request data.
* *
* @return bool * @return bool
* @throws \Exception On error. * @throws Exception On error.
*/ */
private function handle_data(): bool { private function handle_data(): bool {
$data = $this->request_data->read_request( $this->nonce() ); $data = $this->request_data->read_request( $this->nonce() );
@ -234,7 +247,7 @@ class ChangeCartEndpoint implements EndpointInterface {
* @param int $quantity The Quantity. * @param int $quantity The Quantity.
* *
* @return bool * @return bool
* @throws \Exception When product could not be added. * @throws Exception When product could not be added.
*/ */
private function add_product( \WC_Product $product, int $quantity ): bool { private function add_product( \WC_Product $product, int $quantity ): bool {
return false !== $this->cart->add_to_cart( $product->get_id(), $quantity ); return false !== $this->cart->add_to_cart( $product->get_id(), $quantity );
@ -249,7 +262,7 @@ class ChangeCartEndpoint implements EndpointInterface {
* @param array $post_variations The variations. * @param array $post_variations The variations.
* *
* @return bool * @return bool
* @throws \Exception When product could not be added. * @throws Exception When product could not be added.
*/ */
private function add_variable_product( private function add_variable_product(
\WC_Product $product, \WC_Product $product,

View file

@ -9,6 +9,8 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Button\Endpoint; namespace WooCommerce\PayPalCommerce\Button\Endpoint;
use Exception;
use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Order; use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Payer; use WooCommerce\PayPalCommerce\ApiClient\Entity\Payer;
@ -102,6 +104,13 @@ class CreateOrderEndpoint implements EndpointInterface {
*/ */
private $purchase_units; private $purchase_units;
/**
* The logger.
*
* @var LoggerInterface
*/
protected $logger;
/** /**
* CreateOrderEndpoint constructor. * CreateOrderEndpoint constructor.
* *
@ -113,6 +122,7 @@ class CreateOrderEndpoint implements EndpointInterface {
* @param SessionHandler $session_handler The SessionHandler object. * @param SessionHandler $session_handler The SessionHandler object.
* @param Settings $settings The Settings object. * @param Settings $settings The Settings object.
* @param EarlyOrderHandler $early_order_handler The EarlyOrderHandler object. * @param EarlyOrderHandler $early_order_handler The EarlyOrderHandler object.
* @param LoggerInterface $logger The logger.
*/ */
public function __construct( public function __construct(
RequestData $request_data, RequestData $request_data,
@ -122,7 +132,8 @@ class CreateOrderEndpoint implements EndpointInterface {
PayerFactory $payer_factory, PayerFactory $payer_factory,
SessionHandler $session_handler, SessionHandler $session_handler,
Settings $settings, Settings $settings,
EarlyOrderHandler $early_order_handler EarlyOrderHandler $early_order_handler,
LoggerInterface $logger
) { ) {
$this->request_data = $request_data; $this->request_data = $request_data;
@ -133,6 +144,7 @@ class CreateOrderEndpoint implements EndpointInterface {
$this->session_handler = $session_handler; $this->session_handler = $session_handler;
$this->settings = $settings; $this->settings = $settings;
$this->early_order_handler = $early_order_handler; $this->early_order_handler = $early_order_handler;
$this->logger = $logger;
} }
/** /**
@ -190,6 +202,8 @@ class CreateOrderEndpoint implements EndpointInterface {
wp_send_json_success( $order->to_array() ); wp_send_json_success( $order->to_array() );
return true; return true;
} catch ( \RuntimeException $error ) { } catch ( \RuntimeException $error ) {
$this->logger->error( 'Order creation failed: ' . $error->getMessage() );
wp_send_json_error( wp_send_json_error(
array( array(
'name' => is_a( $error, PayPalApiException::class ) ? $error->name() : '', 'name' => is_a( $error, PayPalApiException::class ) ? $error->name() : '',
@ -198,7 +212,9 @@ class CreateOrderEndpoint implements EndpointInterface {
'details' => is_a( $error, PayPalApiException::class ) ? $error->details() : array(), 'details' => is_a( $error, PayPalApiException::class ) ? $error->details() : array(),
) )
); );
} catch ( \Exception $exception ) { } catch ( Exception $exception ) {
$this->logger->error( 'Order creation failed: ' . $exception->getMessage() );
wc_add_notice( $exception->getMessage(), 'error' ); wc_add_notice( $exception->getMessage(), 'error' );
} }
@ -212,11 +228,16 @@ class CreateOrderEndpoint implements EndpointInterface {
* @param \WP_Error $errors The errors, which occurred. * @param \WP_Error $errors The errors, which occurred.
* *
* @return array * @return array
* @throws Exception On Error.
*/ */
public function after_checkout_validation( array $data, \WP_Error $errors ): array { public function after_checkout_validation( array $data, \WP_Error $errors ): array {
if ( ! $errors->errors ) { if ( ! $errors->errors ) {
try {
$order = $this->create_paypal_order(); $order = $this->create_paypal_order();
} catch ( Exception $exception ) {
$this->logger->error( 'Order creation failed: ' . $exception->getMessage() );
throw $exception;
}
/** /**
* In case we are onboarded and everything is fine with the \WC_Order * In case we are onboarded and everything is fine with the \WC_Order
@ -231,6 +252,8 @@ class CreateOrderEndpoint implements EndpointInterface {
return $data; return $data;
} }
$this->logger->error( 'Checkout validation failed: ' . $errors->get_error_message() );
wp_send_json_error( wp_send_json_error(
array( array(
'name' => '', 'name' => '',
@ -336,7 +359,7 @@ class CreateOrderEndpoint implements EndpointInterface {
* *
* @param string $form_values The values of the form. * @param string $form_values The values of the form.
* *
* @throws \Exception On Error. * @throws Exception On Error.
*/ */
private function process_checkout_form( string $form_values ) { private function process_checkout_form( string $form_values ) {
$form_values = explode( '&', $form_values ); $form_values = explode( '&', $form_values );
@ -386,7 +409,7 @@ class CreateOrderEndpoint implements EndpointInterface {
* *
* @param string $form_values The values of the form. * @param string $form_values The values of the form.
* @param \WC_Order|null $wc_order WC order to get data from. * @param \WC_Order|null $wc_order WC order to get data from.
* @throws \Exception On Error. * @throws Exception On Error.
*/ */
private function process_checkout_form_when_creating_account( string $form_values, \WC_Order $wc_order = null ) { private function process_checkout_form_when_creating_account( string $form_values, \WC_Order $wc_order = null ) {
$form_values = explode( '&', $form_values ); $form_values = explode( '&', $form_values );
@ -406,7 +429,12 @@ class CreateOrderEndpoint implements EndpointInterface {
'woocommerce_after_checkout_validation', 'woocommerce_after_checkout_validation',
function ( array $data, \WP_Error $errors ) use ( $wc_order ) { function ( array $data, \WP_Error $errors ) use ( $wc_order ) {
if ( ! $errors->errors ) { if ( ! $errors->errors ) {
$order = $this->create_paypal_order( $wc_order ); try {
$order = $this->create_paypal_order( $wc_order );
} catch ( Exception $exception ) {
$this->logger->error( 'Order creation failed: ' . $exception->getMessage() );
throw $exception;
}
wp_send_json_success( $order->to_array() ); wp_send_json_success( $order->to_array() );
return true; return true;
} }

View file

@ -9,6 +9,8 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Button\Endpoint; namespace WooCommerce\PayPalCommerce\Button\Endpoint;
use Exception;
use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\IdentityToken; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\IdentityToken;
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException; use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException; use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
@ -35,19 +37,29 @@ class DataClientIdEndpoint implements EndpointInterface {
*/ */
private $identity_token; private $identity_token;
/**
* The logger.
*
* @var LoggerInterface
*/
protected $logger;
/** /**
* DataClientIdEndpoint constructor. * DataClientIdEndpoint constructor.
* *
* @param RequestData $request_data The Request Data Helper. * @param RequestData $request_data The Request Data Helper.
* @param IdentityToken $identity_token The Identity Token. * @param IdentityToken $identity_token The Identity Token.
* @param LoggerInterface $logger The logger.
*/ */
public function __construct( public function __construct(
RequestData $request_data, RequestData $request_data,
IdentityToken $identity_token IdentityToken $identity_token,
LoggerInterface $logger
) { ) {
$this->request_data = $request_data; $this->request_data = $request_data;
$this->identity_token = $identity_token; $this->identity_token = $identity_token;
$this->logger = $logger;
} }
/** /**
@ -77,7 +89,9 @@ class DataClientIdEndpoint implements EndpointInterface {
) )
); );
return true; return true;
} catch ( RuntimeException $error ) { } catch ( Exception $error ) {
$this->logger->error( 'Client ID retrieval failed: ' . $error->getMessage() );
wp_send_json_error( wp_send_json_error(
array( array(
'name' => is_a( $error, PayPalApiException::class ) ? $error->name() : '', 'name' => is_a( $error, PayPalApiException::class ) ? $error->name() : '',

View file

@ -169,6 +169,7 @@ return array(
$login_seller_sandbox = $container->get( 'api.endpoint.login-seller-sandbox' ); $login_seller_sandbox = $container->get( 'api.endpoint.login-seller-sandbox' );
$partner_referrals_data = $container->get( 'api.repository.partner-referrals-data' ); $partner_referrals_data = $container->get( 'api.repository.partner-referrals-data' );
$settings = $container->get( 'wcgateway.settings' ); $settings = $container->get( 'wcgateway.settings' );
$logger = $container->get( 'woocommerce.logger.woocommerce' );
$cache = new Cache( 'ppcp-paypal-bearer' ); $cache = new Cache( 'ppcp-paypal-bearer' );
return new LoginSellerEndpoint( return new LoginSellerEndpoint(
@ -177,7 +178,8 @@ return array(
$login_seller_sandbox, $login_seller_sandbox,
$partner_referrals_data, $partner_referrals_data,
$settings, $settings,
$cache $cache,
$logger
); );
}, },
'api.endpoint.partner-referrals-sandbox' => static function ( $container ) : PartnerReferrals { 'api.endpoint.partner-referrals-sandbox' => static function ( $container ) : PartnerReferrals {

View file

@ -9,6 +9,8 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Onboarding\Endpoint; namespace WooCommerce\PayPalCommerce\Onboarding\Endpoint;
use Exception;
use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\PayPalBearer; use WooCommerce\PayPalCommerce\ApiClient\Authentication\PayPalBearer;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\LoginSeller; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\LoginSeller;
use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache; use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache;
@ -67,6 +69,13 @@ class LoginSellerEndpoint implements EndpointInterface {
*/ */
private $cache; private $cache;
/**
* The logger.
*
* @var LoggerInterface
*/
protected $logger;
/** /**
* LoginSellerEndpoint constructor. * LoginSellerEndpoint constructor.
* *
@ -76,6 +85,7 @@ class LoginSellerEndpoint implements EndpointInterface {
* @param PartnerReferralsData $partner_referrals_data The Partner Referrals Data. * @param PartnerReferralsData $partner_referrals_data The Partner Referrals Data.
* @param Settings $settings The Settings. * @param Settings $settings The Settings.
* @param Cache $cache The Cache. * @param Cache $cache The Cache.
* @param LoggerInterface $logger The logger.
*/ */
public function __construct( public function __construct(
RequestData $request_data, RequestData $request_data,
@ -83,7 +93,8 @@ class LoginSellerEndpoint implements EndpointInterface {
LoginSeller $login_seller_sandbox, LoginSeller $login_seller_sandbox,
PartnerReferralsData $partner_referrals_data, PartnerReferralsData $partner_referrals_data,
Settings $settings, Settings $settings,
Cache $cache Cache $cache,
LoggerInterface $logger
) { ) {
$this->request_data = $request_data; $this->request_data = $request_data;
@ -92,6 +103,7 @@ class LoginSellerEndpoint implements EndpointInterface {
$this->partner_referrals_data = $partner_referrals_data; $this->partner_referrals_data = $partner_referrals_data;
$this->settings = $settings; $this->settings = $settings;
$this->cache = $cache; $this->cache = $cache;
$this->logger = $logger;
} }
/** /**
@ -141,7 +153,8 @@ class LoginSellerEndpoint implements EndpointInterface {
); );
wp_send_json_success(); wp_send_json_success();
return true; return true;
} catch ( \RuntimeException $error ) { } catch ( Exception $error ) {
$this->logger->error( 'Onboarding completion handling error: ' . $error->getMessage() );
wp_send_json_error( $error->getMessage() ); wp_send_json_error( $error->getMessage() );
return false; return false;
} }

View file

@ -8,6 +8,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Entity\PurchaseUnit;
use WooCommerce\PayPalCommerce\ApiClient\Repository\CartRepository; use WooCommerce\PayPalCommerce\ApiClient\Repository\CartRepository;
use WooCommerce\PayPalCommerce\TestCase; use WooCommerce\PayPalCommerce\TestCase;
use Mockery; use Mockery;
use WooCommerce\WooCommerce\Logging\Logger\NullLogger;
use function Brain\Monkey\Functions\expect; use function Brain\Monkey\Functions\expect;
class ChangeCartEndpointTest extends TestCase class ChangeCartEndpointTest extends TestCase
@ -68,7 +69,8 @@ class ChangeCartEndpointTest extends TestCase
$shipping, $shipping,
$requestData, $requestData,
$cartRepository, $cartRepository,
$dataStore $dataStore,
new NullLogger()
); );
expect('wp_send_json_success') expect('wp_send_json_success')
@ -171,4 +173,4 @@ class ChangeCartEndpointTest extends TestCase
return $testData; return $testData;
} }
} }

View file

@ -16,6 +16,7 @@ use WooCommerce\PayPalCommerce\Session\SessionHandler;
use WooCommerce\PayPalCommerce\TestCase; use WooCommerce\PayPalCommerce\TestCase;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings; use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
use WooCommerce\WooCommerce\Logging\Logger\NullLogger;
use function Brain\Monkey\Functions\expect; use function Brain\Monkey\Functions\expect;
class CreateOrderEndpointTest extends TestCase class CreateOrderEndpointTest extends TestCase
@ -160,7 +161,8 @@ class CreateOrderEndpointTest extends TestCase
$payer_factory, $payer_factory,
$session_handler, $session_handler,
$settings, $settings,
$early_order_handler $early_order_handler,
new NullLogger()
); );
return array($payer_factory, $testee); return array($payer_factory, $testee);
} }

View file

@ -8,6 +8,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Entity\Token;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException; use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\TestCase; use WooCommerce\PayPalCommerce\TestCase;
use Mockery; use Mockery;
use WooCommerce\WooCommerce\Logging\Logger\NullLogger;
use function Brain\Monkey\Functions\when; use function Brain\Monkey\Functions\when;
use function Brain\Monkey\Functions\expect; use function Brain\Monkey\Functions\expect;
@ -22,7 +23,7 @@ class DataClientIdEndpointTest extends TestCase
parent::setUp(); parent::setUp();
$this->requestData = Mockery::mock(RequestData::class); $this->requestData = Mockery::mock(RequestData::class);
$this->identityToken = Mockery::mock(IdentityToken::class); $this->identityToken = Mockery::mock(IdentityToken::class);
$this->sut = new DataClientIdEndpoint($this->requestData, $this->identityToken); $this->sut = new DataClientIdEndpoint($this->requestData, $this->identityToken, new NullLogger());
} }
public function testHandleRequestSuccess() public function testHandleRequestSuccess()