PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.10.22
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.10.22
1.10.25 1.10.24 1.10.23 1.10.22 1.10.21 1.10.20 1.10.19 1.10.18 1.10.17 1.10.16 1.10.15 1.10.13 1.10.14 1.10.12 1.10.11 1.10.10 1.10.9 1.10.8 untagged-3d9b7ccddc54df87c672 1.10.7 1.10.6 1.10.5 1.10.3 1.10.4 1.10.2 All 169 releases
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

108 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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