prestashop/module-lib-error-handler

自动将模块异常发送到Sentry

1.0.0 2022-05-04 08:26 UTC

This package is auto-updated.

Last update: 2024-09-20 00:16:03 UTC


README

此存储库包含用于在您的PrestaShop模块中捕获错误并将其发送到Sentry的handler库。

先决条件

您应仅在PrestaShop环境中,并且使用至少PHP 7.1.3版本安装此库。

安装

composer require prestashop/module-lib-error-handler

使用

  • 要捕获模块中的所有未处理异常,您需要在主类中初始化错误处理器
    new PrestaShop\Sentry\Handler\Errorhandler($dsn, $module->getLocalPath());
  • 如果您想添加自定义设置,如标签、用户和级别,您应该创建自己的ErrorHandler并扩展\PrestaShop\Sentry\Handler\ErrorHandler
class ErrorHandler extends \PrestaShop\Sentry\Handler\ErrorHandler
{
    /**
     * @var ?Raven_Client
     */
    protected $client;

    public function __construct(Module $module, Env $env)
    {
        parent::__construct($env->get('SENTRY_CREDENTIALS'), $module->getLocalPath());
        
          $this->setUser(
            [
                'id' => Configuration::get('PS_SHOP_EMAIL'),
                'name' => Configuration::get('PS_SHOP_EMAIL')
            ],
            true
        );
        
        $this->setLevel(\Sentry\Severity::warning());
        
             $this->setTags(        
             [
                'ps_version' => $module->version,
                'ps_version' => $psAccounts ? $psAccounts->version : false,
                'php_version' => phpversion(),
                'prestashop_version' => _PS_VERSION_,
                'ps_is_enabled' => (int) Module::isEnabled($module->name),
                'ps_is_installed' => (int) Module::isInstalled($module->name),
                'env' => $env->get('SENTRY_ENVIRONMENT')
            ]
        );
    }
}
  • 您也可以通过使用sentry handle来发送捕获到的异常
        $this->errorHandler = $this->module->getService(ErrorHandler::class);
        try {
            throw new ModuleVersionException('test exception');
        } catch (ModuleVersionException $exception) {
            $this->errorHandler->handle($exception);
            return;
        }
  • 此外,还有一些额外函数可以设置默认模块标签到Sentry
    $this->setModuleInfo(Module $module);