Skip to content

Commit

Permalink
fix: error handler (#406)
Browse files Browse the repository at this point in the history
  • Loading branch information
fox-john authored Jan 15, 2025
1 parent 934aaa5 commit dcedeaf
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 15 deletions.
42 changes: 31 additions & 11 deletions src/Handler/ErrorHandler/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -117,6 +132,11 @@ public function handle($exception)
throw $exception;
} else {
$this->client->captureException($exception);

if ($silent) {
return;
}

CommonService::exitWithExceptionMessage($exception);
}
}
Expand Down
29 changes: 28 additions & 1 deletion src/Service/PsAccountsAdapterService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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_')) {
Expand All @@ -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');
}

Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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;
}
}
Expand All @@ -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 '';
}
}
Expand All @@ -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 '';
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/ServiceContainer/Provider/CommonProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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')
);
Expand Down

0 comments on commit dcedeaf

Please sign in to comment.