vendor/shopware/core/Framework/DataAbstractionLayer/Write/WriteException.php line 9

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\DataAbstractionLayer\Write;
  3. use Shopware\Core\Framework\Api\EventListener\ErrorResponseFactory;
  4. use Shopware\Core\Framework\ShopwareHttpException;
  5. use Symfony\Component\HttpFoundation\Response;
  6. class WriteException extends ShopwareHttpException
  7. {
  8.     private const MESSAGE "There are {{ errorCount }} error(s) while writing data.\n\n{{ messagesString }}";
  9.     /**
  10.      * @var \Throwable[]
  11.      */
  12.     private $exceptions = [];
  13.     public function __construct()
  14.     {
  15.         parent::__construct(self::MESSAGE, ['errorCount' => 0]);
  16.     }
  17.     public function add(\Throwable $exception): WriteException
  18.     {
  19.         $this->exceptions[] = $exception;
  20.         $this->updateMessage();
  21.         return $this;
  22.     }
  23.     public function getExceptions(): array
  24.     {
  25.         return $this->exceptions;
  26.     }
  27.     /**
  28.      * @throws WriteException
  29.      */
  30.     public function tryToThrow(): void
  31.     {
  32.         if (\count($this->exceptions)) {
  33.             throw $this;
  34.         }
  35.     }
  36.     public function getErrorCode(): string
  37.     {
  38.         return 'FRAMEWORK__WRITE_ERROR';
  39.     }
  40.     public function getStatusCode(): int
  41.     {
  42.         return Response::HTTP_BAD_REQUEST;
  43.     }
  44.     public function getErrors(bool $withTrace false): \Generator
  45.     {
  46.         foreach ($this->getExceptions() as $innerException) {
  47.             if ($innerException instanceof ShopwareHttpException) {
  48.                 yield from $innerException->getErrors($withTrace);
  49.                 continue;
  50.             }
  51.             $errorFactory = new ErrorResponseFactory();
  52.             yield from $errorFactory->getErrorsFromException($innerException$withTrace);
  53.         }
  54.     }
  55.     private function updateMessage(): void
  56.     {
  57.         $messages = [];
  58.         foreach ($this->getErrors() as $index => $error) {
  59.             $pointer $error['source']['pointer'] ?? '/';
  60.             $messages[] = sprintf('%d. [%s] %s'$index 1$pointer$error['detail']);
  61.         }
  62.         $messagesString implode(\PHP_EOL$messages);
  63.         $this->parameters = [
  64.             'errorCount' => \count($this->exceptions),
  65.             'messages' => $messages,
  66.             'messagesString' => $messagesString,
  67.         ];
  68.         $this->message $this->parse(self::MESSAGE$this->parameters);
  69.     }
  70. }