You are here

protected function EntityAccessDeniedHttpExceptionNormalizer::buildErrorObjects in JSON:API 8

Same name and namespace in other branches
  1. 8.2 src/Normalizer/EntityAccessDeniedHttpExceptionNormalizer.php \Drupal\jsonapi\Normalizer\EntityAccessDeniedHttpExceptionNormalizer::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.

Overrides HttpExceptionNormalizer::buildErrorObjects

File

src/Normalizer/EntityAccessDeniedHttpExceptionNormalizer.php, line 29

Class

EntityAccessDeniedHttpExceptionNormalizer
Normalizes an EntityAccessDeniedException.

Namespace

Drupal\jsonapi\Normalizer

Code

protected function buildErrorObjects(HttpException $exception) {
  $errors = parent::buildErrorObjects($exception);
  if ($exception instanceof EntityAccessDeniedHttpException) {
    $error = $exception
      ->getError();

    /** @var \Drupal\Core\Entity\EntityInterface $entity */
    $entity = $error['entity'];
    $pointer = $error['pointer'];
    $reason = $error['reason'];
    if (isset($entity)) {
      $errors[0]['id'] = sprintf('/%s--%s/%s', $entity
        ->getEntityTypeId(), $entity
        ->bundle(), $entity
        ->uuid());
    }
    $errors[0]['source']['pointer'] = $pointer;
    if ($reason) {
      $errors[0]['detail'] = isset($errors[0]['detail']) ? $errors[0]['detail'] . ' ' . $reason : $reason;
    }
  }
  return $errors;
}