You are here

protected function HttpExceptionNormalizer::buildErrorObjects in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/jsonapi/src/Normalizer/HttpExceptionNormalizer.php \Drupal\jsonapi\Normalizer\HttpExceptionNormalizer::buildErrorObjects()

Builds the normalized JSON:API error objects for the response.

Parameters

\Symfony\Component\HttpKernel\Exception\HttpException $exception: The Exception.

Return value

array The error objects to include in the response.

3 calls to HttpExceptionNormalizer::buildErrorObjects()
EntityAccessDeniedHttpExceptionNormalizer::buildErrorObjects in core/modules/jsonapi/src/Normalizer/EntityAccessDeniedHttpExceptionNormalizer.php
Builds the normalized JSON:API error objects for the response.
HttpExceptionNormalizer::normalize in core/modules/jsonapi/src/Normalizer/HttpExceptionNormalizer.php
Normalizes an object into a set of arrays/scalars.
UnprocessableHttpEntityExceptionNormalizer::buildErrorObjects in core/modules/jsonapi/src/Normalizer/UnprocessableHttpEntityExceptionNormalizer.php
Builds the normalized JSON:API error objects for the response.
2 methods override HttpExceptionNormalizer::buildErrorObjects()
EntityAccessDeniedHttpExceptionNormalizer::buildErrorObjects in core/modules/jsonapi/src/Normalizer/EntityAccessDeniedHttpExceptionNormalizer.php
Builds the normalized JSON:API error objects for the response.
UnprocessableHttpEntityExceptionNormalizer::buildErrorObjects in core/modules/jsonapi/src/Normalizer/UnprocessableHttpEntityExceptionNormalizer.php
Builds the normalized JSON:API error objects for the response.

File

core/modules/jsonapi/src/Normalizer/HttpExceptionNormalizer.php, line 66

Class

HttpExceptionNormalizer
Normalizes an HttpException in compliance with the JSON:API specification.

Namespace

Drupal\jsonapi\Normalizer

Code

protected function buildErrorObjects(HttpException $exception) {
  $error = [];
  $status_code = $exception
    ->getStatusCode();
  if (!empty(Response::$statusTexts[$status_code])) {
    $error['title'] = Response::$statusTexts[$status_code];
  }
  $error += [
    'status' => (string) $status_code,
    'detail' => $exception
      ->getMessage(),
  ];
  $error['links']['via']['href'] = \Drupal::request()
    ->getUri();

  // Provide an "info" link by default: if the exception carries a single
  // "Link" header, use that, otherwise fall back to the HTTP spec section
  // covering the exception's status code.
  $headers = $exception
    ->getHeaders();
  if (isset($headers['Link']) && !is_array($headers['Link'])) {
    $error['links']['info']['href'] = $headers['Link'];
  }
  elseif ($info_url = $this
    ->getInfoUrl($status_code)) {
    $error['links']['info']['href'] = $info_url;
  }

  // Exceptions thrown without an explicitly defined code get assigned zero by
  // default. Since this is no helpful information, omit it.
  if ($exception
    ->getCode() !== 0) {
    $error['code'] = (string) $exception
      ->getCode();
  }
  if ($this->currentUser
    ->hasPermission('access site reports')) {

    // The following information may contain sensitive information. Only show
    // it to authorized users.
    $error['source'] = [
      'file' => $exception
        ->getFile(),
      'line' => $exception
        ->getLine(),
    ];
    $error['meta'] = [
      'exception' => (string) $exception,
      'trace' => $exception
        ->getTrace(),
    ];
  }
  return [
    $error,
  ];
}