You are here

private function HttpKernel::handleException in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/symfony/http-kernel/HttpKernel.php \Symfony\Component\HttpKernel\HttpKernel::handleException()

Handles an exception by trying to convert it to a Response.

Parameters

\Exception $e An \Exception instance:

Request $request A Request instance:

int $type The type of the request:

Return value

Response A Response instance

Throws

\Exception

2 calls to HttpKernel::handleException()
HttpKernel::handle in vendor/symfony/http-kernel/HttpKernel.php
Handles a Request to convert it to a Response.
HttpKernel::terminateWithException in vendor/symfony/http-kernel/HttpKernel.php
@internal

File

vendor/symfony/http-kernel/HttpKernel.php, line 213

Class

HttpKernel
HttpKernel notifies events to convert a Request object to a Response one.

Namespace

Symfony\Component\HttpKernel

Code

private function handleException(\Exception $e, $request, $type) {
  $event = new GetResponseForExceptionEvent($this, $request, $type, $e);
  $this->dispatcher
    ->dispatch(KernelEvents::EXCEPTION, $event);

  // a listener might have replaced the exception
  $e = $event
    ->getException();
  if (!$event
    ->hasResponse()) {
    $this
      ->finishRequest($request, $type);
    throw $e;
  }
  $response = $event
    ->getResponse();

  // the developer asked for a specific status code
  if ($response->headers
    ->has('X-Status-Code')) {
    $response
      ->setStatusCode($response->headers
      ->get('X-Status-Code'));
    $response->headers
      ->remove('X-Status-Code');
  }
  elseif (!$response
    ->isClientError() && !$response
    ->isServerError() && !$response
    ->isRedirect()) {

    // ensure that we actually have an error response
    if ($e instanceof HttpExceptionInterface) {

      // keep the HTTP status code and headers
      $response
        ->setStatusCode($e
        ->getStatusCode());
      $response->headers
        ->add($e
        ->getHeaders());
    }
    else {
      $response
        ->setStatusCode(500);
    }
  }
  try {
    return $this
      ->filterResponse($response, $request, $type);
  } catch (\Exception $e) {
    return $response;
  }
}