woocommerce-pos
/
vendor_prefixed
/
sentry
/
sentry
/
src
/
Monolog
/
ExceptionToSentryIssueHandler.php
ExceptionToSentryIssueHandler.php in WCPOS – Point of Sale (POS) plugin for WooCommerce 1.10.22, at vendor_prefixed/sentry/sentry/src/Monolog/ExceptionToSentryIssueHandler.php
| 1 | <?php |
| 2 | |
| 3 | declare (strict_types=1); |
| 4 | namespace WCPOS\Vendor\Sentry\Monolog; |
| 5 | |
| 6 | use WCPOS\Vendor\Monolog\Handler\AbstractHandler; |
| 7 | use WCPOS\Vendor\Monolog\Level; |
| 8 | use WCPOS\Vendor\Monolog\Logger; |
| 9 | use WCPOS\Vendor\Monolog\LogRecord; |
| 10 | use WCPOS\Vendor\Psr\Log\LogLevel; |
| 11 | use WCPOS\Vendor\Sentry\State\HubInterface; |
| 12 | use WCPOS\Vendor\Sentry\State\Scope; |
| 13 | /** |
| 14 | * This Monolog handler will collect monolog events and send them to sentry. |
| 15 | */ |
| 16 | class ExceptionToSentryIssueHandler extends AbstractHandler |
| 17 | { |
| 18 | /** |
| 19 | * @var HubInterface |
| 20 | */ |
| 21 | private $hub; |
| 22 | /** |
| 23 | * @phpstan-param value-of<Level::VALUES>|value-of<Level::NAMES>|Level|LogLevel::* $level |
| 24 | */ |
| 25 | public function __construct(HubInterface $hub, $level = Logger::DEBUG, bool $bubble = \true) |
| 26 | { |
| 27 | $this->hub = $hub; |
| 28 | parent::__construct($level, $bubble); |
| 29 | } |
| 30 | /** |
| 31 | * @param array<string, mixed>|LogRecord $record |
| 32 | */ |
| 33 | public function handle($record) : bool |
| 34 | { |
| 35 | $exception = $this->getExceptionFromRecord($record); |
| 36 | /** @phpstan-ignore-next-line */ |
| 37 | if ($exception === null || !$this->isHandling($record)) { |
| 38 | return \false; |
| 39 | } |
| 40 | $this->hub->withScope(function (Scope $scope) use($record, $exception) : void { |
| 41 | $scope->setExtra('monolog.channel', $record['channel']); |
| 42 | $scope->setExtra('monolog.level', $record['level_name']); |
| 43 | $scope->setExtra('monolog.message', $record['message']); |
| 44 | $monologContextData = $this->getMonologContextData($this->getContextFromRecord($record)); |
| 45 | if ($monologContextData !== []) { |
| 46 | $scope->setExtra('monolog.context', $monologContextData); |
| 47 | } |
| 48 | $monologExtraData = $this->getExtraFromRecord($record); |
| 49 | if ($monologExtraData !== []) { |
| 50 | $scope->setExtra('monolog.extra', $monologExtraData); |
| 51 | } |
| 52 | $this->hub->captureException($exception); |
| 53 | }); |
| 54 | return $this->bubble === \false; |
| 55 | } |
| 56 | /** |
| 57 | * @param array<string, mixed>|LogRecord $record |
| 58 | */ |
| 59 | private function getExceptionFromRecord($record) : ?\Throwable |
| 60 | { |
| 61 | $exception = $this->getContextFromRecord($record)['exception'] ?? null; |
| 62 | if ($exception instanceof \Throwable) { |
| 63 | return $exception; |
| 64 | } |
| 65 | return null; |
| 66 | } |
| 67 | /** |
| 68 | * @param array<string, mixed>|LogRecord $record |
| 69 | * |
| 70 | * @return array<string, mixed> |
| 71 | */ |
| 72 | private function getContextFromRecord($record) : array |
| 73 | { |
| 74 | return $this->getArrayFieldFromRecord($record, 'context'); |
| 75 | } |
| 76 | /** |
| 77 | * @param array<string, mixed>|LogRecord $record |
| 78 | * |
| 79 | * @return array<string, mixed> |
| 80 | */ |
| 81 | private function getExtraFromRecord($record) : array |
| 82 | { |
| 83 | return $this->getArrayFieldFromRecord($record, 'extra'); |
| 84 | } |
| 85 | /** |
| 86 | * @param array<string, mixed>|LogRecord $record |
| 87 | * |
| 88 | * @return array<string, mixed> |
| 89 | */ |
| 90 | private function getArrayFieldFromRecord($record, string $field) : array |
| 91 | { |
| 92 | if (isset($record[$field]) && \is_array($record[$field])) { |
| 93 | return $record[$field]; |
| 94 | } |
| 95 | return []; |
| 96 | } |
| 97 | /** |
| 98 | * @param array<string, mixed> $context |
| 99 | * |
| 100 | * @return array<string, mixed> |
| 101 | */ |
| 102 | private function getMonologContextData(array $context) : array |
| 103 | { |
| 104 | unset($context['exception']); |
| 105 | return $context; |
| 106 | } |
| 107 | } |
| 108 |