You are here

final class EdgeExceptionSubscriber in Apigee Edge 8

Handles uncaught ApiExceptions.

Redirects the user to the Edge error page if an uncaught SDK-level ApiException event appears in the HttpKernel component.

Hierarchy

  • class \Drupal\apigee_edge\EventSubscriber\EdgeExceptionSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of EdgeExceptionSubscriber

1 file declares its use of EdgeExceptionSubscriber
EdgeExceptionSubscriberTest.php in tests/src/Unit/EventSubscriber/EdgeExceptionSubscriberTest.php
1 string reference to 'EdgeExceptionSubscriber'
apigee_edge.services.yml in ./apigee_edge.services.yml
apigee_edge.services.yml
1 service uses EdgeExceptionSubscriber
apigee_edge.exception_subscriber in ./apigee_edge.services.yml
Drupal\apigee_edge\EventSubscriber\EdgeExceptionSubscriber

File

src/EventSubscriber/EdgeExceptionSubscriber.php, line 42

Namespace

Drupal\apigee_edge\EventSubscriber
View source
final class EdgeExceptionSubscriber implements EventSubscriberInterface {

  /**
   * The logger instance.
   *
   * @var \Psr\Log\LoggerInterface
   */
  protected $logger;

  /**
   * The messenger service.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface
   */
  protected $messenger;

  /**
   * The config factory service.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Class Resolver service.
   *
   * @var \Drupal\Core\DependencyInjection\ClassResolverInterface
   */
  protected $classResolver;

  /**
   * The current route match.
   *
   * @var \Drupal\Core\Routing\RouteMatchInterface
   */
  protected $routeMatch;

  /**
   * The available main content renderer services, keyed per format.
   *
   * @var array
   */
  protected $mainContentRenderers;

  /**
   * EdgeExceptionSubscriber constructor.
   *
   * @param \Psr\Log\LoggerInterface $logger
   *   The logger service.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory service.
   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
   *   The messenger service.
   * @param \Drupal\Core\DependencyInjection\ClassResolverInterface $class_resolver
   *   The class resolver service.
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The current route match.
   * @param array $main_content_renderers
   *   The available main content renderer service IDs, keyed by format.
   */
  public function __construct(LoggerInterface $logger, ConfigFactoryInterface $config_factory, MessengerInterface $messenger, ClassResolverInterface $class_resolver, RouteMatchInterface $route_match, array $main_content_renderers) {
    $this->logger = $logger;
    $this->configFactory = $config_factory;
    $this->messenger = $messenger;
    $this->classResolver = $class_resolver;
    $this->routeMatch = $route_match;
    $this->mainContentRenderers = $main_content_renderers;
  }

  /**
   * {@inheritdoc}
   */
  protected static function getPriority() {
    return 1024;
  }

  /**
   * Displays the Edge connection error page.
   *
   * @param \Symfony\Component\HttpKernel\Event\ExceptionEvent|\Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
   *   The exception event.
   */
  public function onException($event) {
    $exception = $event instanceof ExceptionEvent ? $event
      ->getThrowable() : $event
      ->getException();
    if ($exception instanceof ApiException || $exception
      ->getPrevious() instanceof ApiException) {
      $context = Error::decodeException($exception);
      $this->logger
        ->critical('@message %function (line %line of %file). <pre>@backtrace_string</pre>', $context);
      $controller = $this->classResolver
        ->getInstanceFromDefinition(ErrorPageController::class);
      $content = [
        '#title' => $controller
          ->getPageTitle(),
        'content' => $controller
          ->render(),
      ];
      $routeMatch = new RouteMatch('apigee_edge.error_page', new Route('/api-communication-error'));
      $renderer = $this->classResolver
        ->getInstanceFromDefinition($this->mainContentRenderers['html']);

      /* @var \Symfony\Component\HttpFoundation\Response $response */
      $response = $renderer
        ->renderResponse($content, $event
        ->getRequest(), $routeMatch);
      $response
        ->setStatusCode(503);

      // Display additional debug messages.
      if ($this->configFactory
        ->get('apigee_edge.error_page')
        ->get('error_page_debug_messages')) {
        $this->messenger
          ->addError($exception
          ->getMessage());
      }
      $event
        ->setResponse($response);
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[KernelEvents::EXCEPTION][] = [
      'onException',
      static::getPriority(),
    ];
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EdgeExceptionSubscriber::$classResolver protected property Class Resolver service.
EdgeExceptionSubscriber::$configFactory protected property The config factory service.
EdgeExceptionSubscriber::$logger protected property The logger instance.
EdgeExceptionSubscriber::$mainContentRenderers protected property The available main content renderer services, keyed per format.
EdgeExceptionSubscriber::$messenger protected property The messenger service.
EdgeExceptionSubscriber::$routeMatch protected property The current route match.
EdgeExceptionSubscriber::getPriority protected static function
EdgeExceptionSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
EdgeExceptionSubscriber::onException public function Displays the Edge connection error page.
EdgeExceptionSubscriber::__construct public function EdgeExceptionSubscriber constructor.