Problem/Motivation

I'm using Raven to report errors to Sentry. As a first step my release script enables the Maintenance mode, then deploys the new code version, then runs database updates, imports new configs, clears the caches and as a last step disables Maintenance mode. During the release some errors appears for anonymous users. I would like to have an option to prevent errors to Sentry while the site is in Maintenance mode and the current user has no permission "Use the site in maintenance mode"

Remaining tasks

Implementation.

Issue fork raven-3567482

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

tikaszvince created an issue. See original summary.

mfb’s picture

You should not need a feature to provide this logic. Add an event listener for the \Drupal\raven\Event\OptionsAlter event, and set the before_send callback option to a function containing whatever logic you need:

function (\Sentry\Event $event, ?\Sentry\EventHint $hint): ?\Sentry\Event {
}
mfb’s picture

One thing to keep in mind is that drush commands might also run as the anonymous user.

tikaszvince’s picture

something like this?


namespace Drupal\MY_MODULE\EventSubscriber;

use Drupal\Core\DependencyInjection\AutowireTrait;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Site\MaintenanceModeInterface;
use Drupal\raven\Event\OptionsAlter;
use Sentry\Event;
use Sentry\EventHint;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * Raven options alter event subscriber.
 */
class RavenOptionsAlterSubscriber implements EventSubscriberInterface {

  use AutowireTrait;

  public function __construct(
    protected readonly AccountProxyInterface $currentUser,
    protected readonly MaintenanceModeInterface $maintenanceMode,
    protected readonly RouteMatchInterface $routeMatch,
  ) {}

  /**
   * On options alter event handler.
   */
  public function onOptionsAlter(OptionsAlter $event): void {
    if (
      $this->maintenanceMode->applies($this->routeMatch)
      && $this->currentUser->isAnonymous()
    ) {
      $event->options['before_send'] = function (
        Event $event,
        ?EventHint $hint,
      ): ?Event {
        return NULL;
      };
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents(): array {
    return [
      OptionsAlter::class => ['onOptionsAlter'],
    ];
  }

}

mfb’s picture

I typically put logic in the before_send callback itself so it is checked at the last minute, i.e. current maintenance mode, current route, current user, etc.

And, check how logging from drush commands or other CLI scripts is disrupted.

tikaszvince’s picture

Status: Active » Closed (won't fix)

Ok, thank you the tip.
I close this

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

mfb’s picture

Version: 7.3.4 » 7.x-dev
Component: Code » Documentation
Category: Feature request » Task
Status: Closed (won't fix) » Active

Let's keep this open for documentation improvements

mfb’s picture

Status: Active » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.