function HttpExceptionNormalizer::buildErrorObjects

Same name in other branches
  1. 9 core/modules/jsonapi/src/Normalizer/HttpExceptionNormalizer.php \Drupal\jsonapi\Normalizer\HttpExceptionNormalizer::buildErrorObjects()
  2. 8.9.x core/modules/jsonapi/src/Normalizer/HttpExceptionNormalizer.php \Drupal\jsonapi\Normalizer\HttpExceptionNormalizer::buildErrorObjects()
  3. 11.x 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
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 65

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();
    }
    $is_verbose_reporting = \Drupal::config('system.logging')->get('error_level') === ERROR_REPORTING_DISPLAY_VERBOSE;
    $site_report_access = $this->currentUser
        ->hasPermission('access site reports');
    if ($site_report_access && $is_verbose_reporting) {
        // 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,
    ];
}

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