Skip to content
This repository has been archived by the owner on Dec 19, 2019. It is now read-only.

Filter error array by unique values #1023

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 8 additions & 22 deletions app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace Magento\QuoteGraphQl\Model\Cart;

use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\Message\AbstractMessage;
use Magento\Framework\Message\MessageInterface;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Model\Quote;

Expand Down Expand Up @@ -55,29 +55,15 @@ public function execute(Quote $cart, array $cartItems): void
}

if ($cart->getData('has_error')) {
throw new GraphQlInputException(
__('Shopping cart error: %message', ['message' => $this->getCartErrors($cart)])
);
$e = new GraphQlInputException(__('Shopping cart errors'));
$errors = $cart->getErrors();
foreach ($errors as $error) {
/** @var MessageInterface $error */
$e->addError(new GraphQlInputException(__($error->getText())));
}
throw $e;
}

$this->cartRepository->save($cart);
}

/**
* Collecting cart errors
*
* @param Quote $cart
* @return string
*/
private function getCartErrors(Quote $cart): string
{
$errorMessages = [];

/** @var AbstractMessage $error */
foreach ($cart->getErrors() as $error) {
$errorMessages[] = $error->getText();
}

return implode(PHP_EOL, $errorMessages);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@ public function execute(Quote $cart, array $cartItemData): void
}

if (is_string($result)) {
throw new GraphQlInputException(__($result));
$e = new GraphQlInputException(__('Cannot add product to cart'));
$errors = array_unique(explode("\n", $result));
foreach ($errors as $error) {
$e->addError(new GraphQlInputException(__($error)));
}
throw $e;
}
}

Expand Down