From dcedeaf0ff6a07e2c509883097a993ede5dba857 Mon Sep 17 00:00:00 2001 From: Jonathan Renard <1273438+fox-john@users.noreply.github.com> Date: Wed, 15 Jan 2025 18:28:02 +0100 Subject: [PATCH] fix: error handler (#406) --- src/Handler/ErrorHandler/ErrorHandler.php | 42 ++++++++++++++----- src/Service/PsAccountsAdapterService.php | 29 ++++++++++++- .../Provider/CommonProvider.php | 5 +-- 3 files changed, 61 insertions(+), 15 deletions(-) diff --git a/src/Handler/ErrorHandler/ErrorHandler.php b/src/Handler/ErrorHandler/ErrorHandler.php index 0d0ad68d..20606b47 100644 --- a/src/Handler/ErrorHandler/ErrorHandler.php +++ b/src/Handler/ErrorHandler/ErrorHandler.php @@ -27,7 +27,6 @@ namespace PrestaShop\Module\PsEventbus\Handler\ErrorHandler; use PrestaShop\Module\PsEventbus\Service\CommonService; -use PrestaShop\Module\PsEventbus\Service\PsAccountsAdapterService; if (!defined('_PS_VERSION_')) { exit; @@ -44,47 +43,63 @@ class ErrorHandler protected $client; /** - * @param \Ps_eventbus $module - * @param PsAccountsAdapterService $psAccountsAdapterService * @param string $sentryDsn * @param string $sentryEnv * * @return void */ - public function __construct(\Ps_eventbus $module, PsAccountsAdapterService $psAccountsAdapterService, $sentryDsn, $sentryEnv) + public function __construct($sentryDsn, $sentryEnv) { + try { + /** @var \ModuleCore $accountsModule */ + $accountsModule = \Module::getInstanceByName('ps_accounts'); + /** @var mixed $accountService */ + $accountService = $accountsModule->get('PrestaShop\Module\PsAccounts\Service\PsAccountsService'); + + /** @var \ModuleCore $eventbusModule */ + $eventbusModule = \Module::getInstanceByName('ps_eventbus'); + } catch (\Exception $e) { + $accountsModule = null; + $accountService = null; + $eventbusModule = null; + } + try { $this->client = new \Raven_Client( $sentryDsn, [ 'level' => 'warning', 'tags' => [ - 'shop_id' => $psAccountsAdapterService->getShopUuid(), - 'ps_eventbus_version' => $module->version, - 'ps_accounts_version' => $psAccountsAdapterService->getModule() ? $psAccountsAdapterService->getModule()->version : false, + 'shop_id' => $accountService ? $accountService->getShopUuid() : false, + 'ps_eventbus_version' => $eventbusModule ? $eventbusModule->version : false, + 'ps_accounts_version' => $accountsModule ? $accountsModule->version : false, 'php_version' => phpversion(), 'prestashop_version' => _PS_VERSION_, - 'ps_eventbus_is_enabled' => \Module::isEnabled((string) $module->name), - 'ps_eventbus_is_installed' => \Module::isInstalled((string) $module->name), + 'ps_eventbus_is_enabled' => $eventbusModule ? \Module::isEnabled((string) $eventbusModule->name) : false, + 'ps_eventbus_is_installed' => $eventbusModule ? \Module::isInstalled((string) $eventbusModule->name) : false, 'env' => $sentryEnv, ], ] ); /** @var string $configurationPsShopEmail */ $configurationPsShopEmail = \Configuration::get('PS_SHOP_EMAIL'); - $this->client->set_user_data($psAccountsAdapterService->getShopUuid(), $configurationPsShopEmail); + $this->client->set_user_data( + $accountService ? $accountService->getShopUuid() : false, + $configurationPsShopEmail + ); } catch (\Exception $e) { } } /** * @param mixed $exception + * @param mixed $silent * * @return void * * @@throws Exception */ - public function handle($exception) + public function handle($exception, $silent = null) { $logsEnabled = false; $verboseEnabled = false; @@ -117,6 +132,11 @@ public function handle($exception) throw $exception; } else { $this->client->captureException($exception); + + if ($silent) { + return; + } + CommonService::exitWithExceptionMessage($exception); } } diff --git a/src/Service/PsAccountsAdapterService.php b/src/Service/PsAccountsAdapterService.php index 327506e6..ce9cb9ad 100644 --- a/src/Service/PsAccountsAdapterService.php +++ b/src/Service/PsAccountsAdapterService.php @@ -26,6 +26,7 @@ namespace PrestaShop\Module\PsEventbus\Service; +use PrestaShop\Module\PsEventbus\Handler\ErrorHandler\ErrorHandler; use PrestaShop\Module\PsEventbus\Helper\ModuleHelper; if (!defined('_PS_VERSION_')) { @@ -39,14 +40,20 @@ class PsAccountsAdapterService */ private $moduleHelper; + /** + * @var ErrorHandler + */ + private $errorHandler; + /** * @var false|\ModuleCore */ private $psAccountModule; - public function __construct(ModuleHelper $moduleHelper) + public function __construct(ModuleHelper $moduleHelper, ErrorHandler $errorHandler) { $this->moduleHelper = $moduleHelper; + $this->errorHandler = $errorHandler; $this->psAccountModule = $this->moduleHelper->getInstanceByName('ps_accounts'); } @@ -78,6 +85,11 @@ public function getService() try { return $this->psAccountModule->getService('PrestaShop\Module\PsAccounts\Service\PsAccountsService'); } catch (\Exception $e) { + $this->errorHandler->handle( + new \PrestaShopException('Failed to load PsAccountsService', 0, $e), + true + ); + return false; } } @@ -96,6 +108,11 @@ public function getPresenter() try { return $this->psAccountModule->getService('PrestaShop\Module\PsAccounts\Presenter\PsAccountsPresenter'); } catch (\Exception $e) { + $this->errorHandler->handle( + new \PrestaShopException('Failed to load PsAccountsPresenter', 0, $e), + true + ); + return false; } } @@ -114,6 +131,11 @@ public function getShopUuid() try { return $this->getService()->getShopUuid(); } catch (\Exception $e) { + $this->errorHandler->handle( + new \PrestaShopException('Failed to get shop uuid from ps_account', 0, $e), + true + ); + return ''; } } @@ -132,6 +154,11 @@ public function getOrRefreshToken() try { return $this->getService()->getOrRefreshToken(); } catch (\Exception $e) { + $this->errorHandler->handle( + new \PrestaShopException('Failed to get refresh token from ps_account', 0, $e), + true + ); + return ''; } } diff --git a/src/ServiceContainer/Provider/CommonProvider.php b/src/ServiceContainer/Provider/CommonProvider.php index fc013681..ff428f7c 100644 --- a/src/ServiceContainer/Provider/CommonProvider.php +++ b/src/ServiceContainer/Provider/CommonProvider.php @@ -51,7 +51,8 @@ public function provide(ServiceContainer $container) }); $container->registerProvider(PsAccountsAdapterService::class, static function () use ($container) { return new PsAccountsAdapterService( - $container->get(ModuleHelper::class) + $container->get(ModuleHelper::class), + $container->get(ErrorHandler::class) ); }); $container->registerProvider(JsonFormatter::class, static function () { @@ -69,8 +70,6 @@ public function provide(ServiceContainer $container) }); $container->registerProvider(ErrorHandler::class, static function () use ($container) { return new ErrorHandler( - $container->get('ps_eventbus.module'), - $container->get(PsAccountsAdapterService::class), $container->getParameter('ps_eventbus.sentry_dsn'), $container->getParameter('ps_eventbus.sentry_env') );