You are here

protected function DefaultExceptionHtmlSubscriber::makeSubrequest in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Core/EventSubscriber/DefaultExceptionHtmlSubscriber.php \Drupal\Core\EventSubscriber\DefaultExceptionHtmlSubscriber::makeSubrequest()

Makes a subrequest to retrieve the default error page.

Parameters

\Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event: The event to process

string $url: The path/url to which to make a subrequest for this error message.

int $status_code: The status code for the error being handled.

5 calls to DefaultExceptionHtmlSubscriber::makeSubrequest()
CustomPageExceptionHtmlSubscriber::on403 in core/lib/Drupal/Core/EventSubscriber/CustomPageExceptionHtmlSubscriber.php
Handles a 403 error for HTML.
CustomPageExceptionHtmlSubscriber::on404 in core/lib/Drupal/Core/EventSubscriber/CustomPageExceptionHtmlSubscriber.php
Handles a 404 error for HTML.
DefaultExceptionHtmlSubscriber::on401 in core/lib/Drupal/Core/EventSubscriber/DefaultExceptionHtmlSubscriber.php
Handles a 401 error for HTML.
DefaultExceptionHtmlSubscriber::on403 in core/lib/Drupal/Core/EventSubscriber/DefaultExceptionHtmlSubscriber.php
Handles a 403 error for HTML.
DefaultExceptionHtmlSubscriber::on404 in core/lib/Drupal/Core/EventSubscriber/DefaultExceptionHtmlSubscriber.php
Handles a 404 error for HTML.

File

core/lib/Drupal/Core/EventSubscriber/DefaultExceptionHtmlSubscriber.php, line 119
Contains \Drupal\Core\EventSubscriber\DefaultExceptionHtmlSubscriber.

Class

DefaultExceptionHtmlSubscriber
Exception subscriber for handling core default HTML error pages.

Namespace

Drupal\Core\EventSubscriber

Code

protected function makeSubrequest(GetResponseForExceptionEvent $event, $url, $status_code) {
  $request = $event
    ->getRequest();
  $exception = $event
    ->getException();
  if (!($url && $url[0] == '/')) {
    $url = $request
      ->getBasePath() . '/' . $url;
  }
  $current_url = $request
    ->getBasePath() . $request
    ->getPathInfo();
  if ($url != $request
    ->getBasePath() . '/' && $url != $current_url) {
    if ($request
      ->getMethod() === 'POST') {
      $sub_request = Request::create($url, 'POST', $this->redirectDestination
        ->getAsArray() + [
        '_exception_statuscode' => $status_code,
      ] + $request->request
        ->all(), $request->cookies
        ->all(), [], $request->server
        ->all());
    }
    else {
      $sub_request = Request::create($url, 'GET', $request->query
        ->all() + $this->redirectDestination
        ->getAsArray() + [
        '_exception_statuscode' => $status_code,
      ], $request->cookies
        ->all(), [], $request->server
        ->all());
    }
    try {

      // Persist the 'exception' attribute to the subrequest.
      $sub_request->attributes
        ->set('exception', $request->attributes
        ->get('exception'));

      // Persist the access result attribute to the subrequest, so that the
      // error page inherits the access result of the master request.
      $sub_request->attributes
        ->set(AccessAwareRouterInterface::ACCESS_RESULT, $request->attributes
        ->get(AccessAwareRouterInterface::ACCESS_RESULT));

      // Carry over the session to the subrequest.
      if ($session = $request
        ->getSession()) {
        $sub_request
          ->setSession($session);
      }
      $response = $this->httpKernel
        ->handle($sub_request, HttpKernelInterface::SUB_REQUEST);

      // Only 2xx responses should have their status code overridden; any
      // other status code should be passed on: redirects (3xx), error (5xx)…
      // @see https://www.drupal.org/node/2603788#comment-10504916
      if ($response
        ->isSuccessful()) {
        $response
          ->setStatusCode($status_code);
      }

      // Persist any special HTTP headers that were set on the exception.
      if ($exception instanceof HttpExceptionInterface) {
        $response->headers
          ->add($exception
          ->getHeaders());
      }
      $event
        ->setResponse($response);
    } catch (\Exception $e) {

      // If an error happened in the subrequest we can't do much else. Instead,
      // just log it. The DefaultExceptionSubscriber will catch the original
      // exception and handle it normally.
      $error = Error::decodeException($e);
      $this->logger
        ->log($error['severity_level'], '%type: @message in %function (line %line of %file).', $error);
    }
  }
}