custom/plugins/PickwareErpStarter/vendor/pickware/api-error-handling-bundle/src/ControllerExceptionHandling/AdminApiJsonApiErrorExceptionLocalization.php line 42

Open in your IDE?
  1. <?php
  2. /*
  3.  * Copyright (c) Pickware GmbH. All rights reserved.
  4.  * This file is part of software that is released under a proprietary license.
  5.  * You must not copy, modify, distribute, make publicly available, or execute
  6.  * its contents or parts thereof without express permission by the copyright
  7.  * holder, unless otherwise permitted by law.
  8.  */
  9. declare(strict_types=1);
  10. namespace Pickware\ApiErrorHandlingBundle\ControllerExceptionHandling;
  11. use Exception;
  12. use Pickware\HttpUtils\JsonApi\JsonApiError;
  13. use Psr\Log\LoggerInterface;
  14. use Shopware\Core\Framework\Api\Context\AdminApiSource;
  15. use Shopware\Core\Framework\Context;
  16. use Shopware\Core\PlatformRequest;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  19. class AdminApiJsonApiErrorExceptionLocalization implements EventSubscriberInterface
  20. {
  21.     private LoggerInterface $logger;
  22.     public function __construct(LoggerInterface $logger)
  23.     {
  24.         $this->logger $logger;
  25.     }
  26.     public static function getSubscribedEvents(): array
  27.     {
  28.         return [
  29.             ResponseEvent::class => [
  30.                 'onResponseEvent',
  31.                 0,
  32.             ],
  33.         ];
  34.     }
  35.     public function onResponseEvent(ResponseEvent $event): void
  36.     {
  37.         $response $event->getResponse();
  38.         if ($response->getStatusCode() < 400) {
  39.             return;
  40.         }
  41.         /** @var Context $context */
  42.         $context $event->getRequest()->attributes->get(PlatformRequest::ATTRIBUTE_CONTEXT_OBJECT);
  43.         // Only the Admin API uses JSON API.
  44.         if (!$context || !($context->getSource() instanceof AdminApiSource)) {
  45.             return;
  46.         }
  47.         // We catch the error because we don't want to obfuscate the original error
  48.         try {
  49.             $content json_decode($response->getContent(), true512JSON_THROW_ON_ERROR);
  50.         } catch (Exception $error) {
  51.             $this->logger->error(
  52.                 sprintf(
  53.                     'Caught an json decode exception while trying to localize error in %s',
  54.                     self::class,
  55.                 ),
  56.                 [
  57.                     'responseContent' => $response->getContent(),
  58.                     'caughtException' => $error,
  59.                 ],
  60.             );
  61.             return;
  62.         }
  63.         $errors $content['errors'] ?? null;
  64.         if (!$errors) {
  65.             return;
  66.         }
  67.         $locales $event->getRequest()->getLanguages();
  68.         $localizedErrors = [];
  69.         foreach ($errors as $error) {
  70.             if (!$error || !($error['meta'] ?? null) || !($error['meta']['_localizedProperties'] ?? null)) {
  71.                 $localizedErrors[] = $error;
  72.                 continue;
  73.             }
  74.             $localizedErrors[] = (new JsonApiError($error))->getLocalizedCopy($locales);
  75.         }
  76.         $content['errors'] = $localizedErrors;
  77.         $response->setContent(json_encode($content));
  78.         $event->setResponse($response);
  79.     }
  80. }