From 4e9af1362c14a383cf158068aa485cd65ac3b308 Mon Sep 17 00:00:00 2001 From: Dragan Balatinac Date: Wed, 9 Apr 2025 12:24:12 +0200 Subject: [PATCH 1/3] Add PSR-14 event before an exception is sent to sentry via capture function --- Classes/DebugExceptionHandler.php | 17 +++++++- Classes/Event/BeforeSentryCaptureEvent.php | 47 ++++++++++++++++++++++ Classes/ProductionExceptionHandler.php | 18 ++++++++- Configuration/Services.yaml | 8 +++- 4 files changed, 85 insertions(+), 5 deletions(-) create mode 100644 Classes/Event/BeforeSentryCaptureEvent.php diff --git a/Classes/DebugExceptionHandler.php b/Classes/DebugExceptionHandler.php index 2c7f11e..883d10e 100644 --- a/Classes/DebugExceptionHandler.php +++ b/Classes/DebugExceptionHandler.php @@ -2,7 +2,10 @@ namespace Networkteam\SentryClient; +use Networkteam\SentryClient\Event\BeforeSentryCaptureEvent; use Networkteam\SentryClient\Service\SentryService; +use Psr\EventDispatcher\EventDispatcherInterface; +use TYPO3\CMS\Core\Utility\GeneralUtility; class DebugExceptionHandler extends \TYPO3\CMS\Core\Error\DebugExceptionHandler { @@ -18,7 +21,17 @@ public function __construct() */ public function handleException(\Throwable $exception): void { - Client::captureException($exception); + $eventDispatcher = GeneralUtility::makeInstance(EventDispatcherInterface::class); + + $event = $eventDispatcher->dispatch( + new BeforeSentryCaptureEvent($exception) + ); + + if (!$event->isPropagationStopped()) { + $exceptionToSend = $event->getException(); + Client::captureException($exceptionToSend); + } + parent::handleException($exception); } -} \ No newline at end of file +} diff --git a/Classes/Event/BeforeSentryCaptureEvent.php b/Classes/Event/BeforeSentryCaptureEvent.php new file mode 100644 index 0000000..52bc2b7 --- /dev/null +++ b/Classes/Event/BeforeSentryCaptureEvent.php @@ -0,0 +1,47 @@ +exception = $exception; + } + + public function getException(): Throwable + { + return $this->exception; + } + + public function setException(Throwable $exception): void + { + $this->exception = $exception; + } + + public function isPropagationStopped(): bool + { + return $this->propagationStopped; + } + + public function stopPropagation(): void + { + $this->propagationStopped = true; + } +} diff --git a/Classes/ProductionExceptionHandler.php b/Classes/ProductionExceptionHandler.php index 23146fe..7010a9c 100644 --- a/Classes/ProductionExceptionHandler.php +++ b/Classes/ProductionExceptionHandler.php @@ -2,9 +2,12 @@ namespace Networkteam\SentryClient; +use Networkteam\SentryClient\Event\BeforeSentryCaptureEvent; use Networkteam\SentryClient\Service\ConfigurationService; use Networkteam\SentryClient\Service\SentryService; +use Psr\EventDispatcher\EventDispatcherInterface; use Sentry\EventId; +use TYPO3\CMS\Core\Utility\GeneralUtility; class ProductionExceptionHandler extends \TYPO3\CMS\Core\Error\ProductionExceptionHandler { @@ -23,9 +26,20 @@ public function __construct() public function handleException(\Throwable $exception): void { $ignoredCodes = array_merge(self::IGNORED_EXCEPTION_CODES, self::IGNORED_HMAC_EXCEPTION_CODES); + if (!in_array($exception->getCode(), $ignoredCodes, true)) { - $this->eventId = Client::captureException($exception); + $eventDispatcher = GeneralUtility::makeInstance(EventDispatcherInterface::class); + + $event = $eventDispatcher->dispatch( + new BeforeSentryCaptureEvent($exception) + ); + + if (!$event->isPropagationStopped()) { + $exceptionToSend = $event->getException(); + $this->eventId = Client::captureException($exceptionToSend); + } } + parent::handleException($exception); } @@ -54,4 +68,4 @@ protected function writeLog(string $logMessage) parent::writeLog($logMessage); } -} \ No newline at end of file +} diff --git a/Configuration/Services.yaml b/Configuration/Services.yaml index 98678c6..7b3a8d7 100644 --- a/Configuration/Services.yaml +++ b/Configuration/Services.yaml @@ -7,8 +7,14 @@ services: Networkteam\SentryClient\Client: public: true + Networkteam\SentryClient\ProductionExceptionHandler: + public: true + + Networkteam\SentryClient\DebugExceptionHandler: + public: true + Networkteam\SentryClient\EventListener\SystemInformationToolbarCollectorEventListener: tags: - name: event.listener identifier: 'SentryStatus' - event: TYPO3\CMS\Backend\Backend\Event\SystemInformationToolbarCollectorEvent \ No newline at end of file + event: TYPO3\CMS\Backend\Backend\Event\SystemInformationToolbarCollectorEvent From 52cdbce9db729bde69b052def139d50e4f241adb Mon Sep 17 00:00:00 2001 From: Dragan Balatinac Date: Wed, 9 Apr 2025 12:25:57 +0200 Subject: [PATCH 2/3] Remove obsolete services yaml registration --- Configuration/Services.yaml | 6 ------ 1 file changed, 6 deletions(-) diff --git a/Configuration/Services.yaml b/Configuration/Services.yaml index 7b3a8d7..999e058 100644 --- a/Configuration/Services.yaml +++ b/Configuration/Services.yaml @@ -7,12 +7,6 @@ services: Networkteam\SentryClient\Client: public: true - Networkteam\SentryClient\ProductionExceptionHandler: - public: true - - Networkteam\SentryClient\DebugExceptionHandler: - public: true - Networkteam\SentryClient\EventListener\SystemInformationToolbarCollectorEventListener: tags: - name: event.listener From f22cbadfa4a24b03af0e598dc1f5752e3ff731de Mon Sep 17 00:00:00 2001 From: Dragan Balatinac Date: Fri, 11 Apr 2025 11:14:40 +0200 Subject: [PATCH 3/3] Add javascript files to enable usercentrics integration --- .../remove-usercentrics-glitchtip-cookie.js | 11 +++++++++++ .../set-usercentrics-glitchtip-cookie.js | 15 +++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 Resources/Public/JavaScript/remove-usercentrics-glitchtip-cookie.js create mode 100644 Resources/Public/JavaScript/set-usercentrics-glitchtip-cookie.js diff --git a/Resources/Public/JavaScript/remove-usercentrics-glitchtip-cookie.js b/Resources/Public/JavaScript/remove-usercentrics-glitchtip-cookie.js new file mode 100644 index 0000000..2ef2fda --- /dev/null +++ b/Resources/Public/JavaScript/remove-usercentrics-glitchtip-cookie.js @@ -0,0 +1,11 @@ +function removeCookieByName(name) { + const cookieName = name; + const pastDate = 'Thu, 01 Jan 1970 00:00:00 GMT'; + + document.cookie = `${cookieName}=; expires=${pastDate}; path=/; SameSite=Lax; Secure`; + + console.log(`Attempted to remove cookie "${cookieName}". Check browser developer tools to confirm.`); +} + +const cookieNameToRemove = 'allow-glitchtip'; +removeCookieByName(cookieNameToRemove); diff --git a/Resources/Public/JavaScript/set-usercentrics-glitchtip-cookie.js b/Resources/Public/JavaScript/set-usercentrics-glitchtip-cookie.js new file mode 100644 index 0000000..7a64178 --- /dev/null +++ b/Resources/Public/JavaScript/set-usercentrics-glitchtip-cookie.js @@ -0,0 +1,15 @@ +function setPersistentCookie(name, value, days) { + let expires = ""; + if (days) { + const date = new Date(); + date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); + expires = "; expires=" + date.toUTCString(); + } + document.cookie = name + "=" + (value || "") + expires + "; path=/; SameSite=Lax; Secure"; +} + +const cookieName = 'allow-glitchtip'; +const cookieValue = 'true'; +const expirationInDays = 365; + +setPersistentCookie(cookieName, cookieValue, expirationInDays);