You are here

public function CspSubscriber::onCspPolicyAlter in Raven: Sentry Integration 8.2

Same name and namespace in other branches
  1. 3.x src/EventSubscriber/CspSubscriber.php \Drupal\raven\EventSubscriber\CspSubscriber::onCspPolicyAlter()

Alter CSP policy to allow Sentry to send JS errors.

Parameters

\Drupal\csp\Event\PolicyAlterEvent $alterEvent: The Policy Alter event.

File

src/EventSubscriber/CspSubscriber.php, line 51

Class

CspSubscriber
Subscriber for CSP events.

Namespace

Drupal\raven\EventSubscriber

Code

public function onCspPolicyAlter(PolicyAlterEvent $alterEvent) {
  $config = $this->configFactory
    ->get('raven.settings');
  if (!$config
    ->get('javascript_error_handler')) {
    return;
  }
  if (!class_exists(\Raven_Client::class)) {
    return;
  }
  try {
    $dsn = \Raven_Client::parseDSN(empty($_SERVER['SENTRY_DSN']) ? $config
      ->get('public_dsn') : $_SERVER['SENTRY_DSN']);
  } catch (\InvalidArgumentException $e) {

    // Raven is incorrectly configured.
    return;
  }
  self::fallbackAwareAppendIfEnabled($alterEvent
    ->getPolicy(), 'connect-src', [
    $dsn['server'],
    str_replace('/store/', '/envelope/', $dsn['server']),
  ]);
}