You are here

raven_test.module in Raven: Sentry Integration 3.x

Same filename and directory in other branches
  1. 8.2 tests/modules/raven_test.module

Raven test module.

File

tests/modules/raven_test.module
View source
<?php

/**
 * @file
 * Raven test module.
 */

/**
 * Implements hook_page_attachments().
 */
function raven_test_page_attachments(array &$attachments) {
  $client = \Drupal::service('logger.raven')
    ->getClient();
  $client
    ->getOptions()
    ->setBeforeSendCallback(function ($event) use (&$attachments) {
    if (!empty($event
      ->getLogger()) && !empty($event
      ->getMessageFormatted())) {
      $attachments['#attached']['http_header'][] = [
        $event
          ->getLogger(),
        $event
          ->getMessageFormatted(),
        FALSE,
      ];
      $frames = $event
        ->getStacktrace()
        ->getFrames();
      $last_frame = end($frames);
      $attachments['#attached']['http_header'][] = [
        'X-Stacktrace-File',
        $last_frame
          ->getFile(),
        FALSE,
      ];
    }
    if (!empty($event
      ->getExceptions())) {
      if (preg_match('/Allowed memory size of ([0-9]+) bytes exhausted/', $event
        ->getExceptions()[0]
        ->getValue(), $matches)) {
        echo $matches[1];
      }
    }
  });
  \Drupal::logger('X-Logged')
    ->error('Logged');
  \Drupal::logger('X-Not-Logged')
    ->error('Not logged');
  \Drupal::logger('X-Logged')
    ->error('Logged');
  if ($memory_limit = \Drupal::request()->query
    ->get('memory_limit')) {
    ini_set('memory_limit', $memory_limit);
    while (TRUE) {
      $attachments[] = 1;
    }
  }
  $attachments['#cache']['contexts'][] = 'url.query_args:memory_limit';
}

/**
 * Implements hook_raven_filter_alter().
 */
function raven_test_raven_filter_alter(&$filter) {
  if ($filter['context']['channel'] === 'X-Not-Logged') {
    $filter['process'] = FALSE;
  }
}