class RouterTestEarlyExceptionSubscriber

Same name and namespace in other branches
  1. 11.x core/modules/system/tests/modules/router_test_directory/src/RouterTestEarlyExceptionSubscriber.php \Drupal\router_test\RouterTestEarlyExceptionSubscriber

Event subscribers for exceptions thrown in early kernel middleware.

Hierarchy

Expanded class hierarchy of RouterTestEarlyExceptionSubscriber

File

core/modules/system/tests/modules/router_test_directory/src/RouterTestEarlyExceptionSubscriber.php, line 16

Namespace

Drupal\router_test
View source
class RouterTestEarlyExceptionSubscriber implements EventSubscriberInterface {
  
  /**
   * Throw an exception, which will trigger exception-handling subscribers.
   *
   * See DefaultExceptionHtmlSubscriber.
   */
  public function onKernelRequest(RequestEvent $event) : void {
    if ($event->isMainRequest() && $event->getRequest()->headers
      ->get('Authorization') === 'Bearer invalid') {
      throw new HttpException(Response::HTTP_UNAUTHORIZED, 'This is a common exception during authentication.');
    }
  }
  
  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() : array {
    // This is the same priority as AuthenticationSubscriber, however
    // exceptions are not restricted to authentication; this is a common,
    // early point to emulate an exception, e.g. when an OAuth token is
    // rejected.
    $events[KernelEvents::REQUEST][] = [
      'onKernelRequest',
      300,
    ];
    return $events;
  }

}

Members

Title Sort descending Modifiers Object type Summary
RouterTestEarlyExceptionSubscriber::getSubscribedEvents public static function
RouterTestEarlyExceptionSubscriber::onKernelRequest public function Throw an exception, which will trigger exception-handling subscribers.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.