Skip to content

Introduce PSR-14 Event for Intercepting and Modifying Data Sent to Sentry #114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions Classes/DebugExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
}
}
}
47 changes: 47 additions & 0 deletions Classes/Event/BeforeSentryCaptureEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Networkteam\SentryClient\Event;

use Psr\EventDispatcher\StoppableEventInterface;
use Throwable;

/**
* Event dispatched just before an exception is sent to Sentry.
*
* Listeners can use this event to:
* - Modify the exception before it's sent.
* - Add specific context/tags to Sentry scope for this exception.
* - Prevent the exception from being sent entirely by calling stopPropagation().
*/
class BeforeSentryCaptureEvent implements StoppableEventInterface
{
private bool $propagationStopped = false;
private Throwable $exception;

public function __construct(Throwable $exception)
{
$this->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;
}
}
18 changes: 16 additions & 2 deletions Classes/ProductionExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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()) {
Copy link
Contributor

@brotkrueml brotkrueml Apr 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO isPropagationStopped() check is used wrong here: the event dispatcher takes care to check if other listeners in the queue should be called (isPropagationStopped() returns true). You use this to avoid the sending of exceptions to Sentry - which is wrong IMHO.

There may be valid use cases in an installation to define two event listeners where the first one says: I am the one, the second shouldn't be called - then modifies the exception for storage in Sentry and sets propagation stopped - and in your implementation the exception is not send to Sentry altogether. Think of an extension plugin which has a token (person-referenced) in a URL or a user id which the appropriate listener want to obfuscate. Other listeners are not needed to be called therefore.

So, I suggest to decouple that and introduce a separate method to disable the sending of exceptions. Maybe the implementation of the stoppable interface is not needed altogether and can be removed.

$exceptionToSend = $event->getException();
$this->eventId = Client::captureException($exceptionToSend);
}
}

parent::handleException($exception);
}

Expand Down Expand Up @@ -54,4 +68,4 @@ protected function writeLog(string $logMessage)

parent::writeLog($logMessage);
}
}
}
2 changes: 1 addition & 1 deletion Configuration/Services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ services:
tags:
- name: event.listener
identifier: 'SentryStatus'
event: TYPO3\CMS\Backend\Backend\Event\SystemInformationToolbarCollectorEvent
event: TYPO3\CMS\Backend\Backend\Event\SystemInformationToolbarCollectorEvent
Original file line number Diff line number Diff line change
@@ -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);
15 changes: 15 additions & 0 deletions Resources/Public/JavaScript/set-usercentrics-glitchtip-cookie.js
Original file line number Diff line number Diff line change
@@ -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);