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

Commit 98df873

Browse files
authored
ENGCOM-6254: Filter error array by unique values #1023
2 parents ba44d97 + 4e9c4de commit 98df873

File tree

2 files changed

+14
-23
lines changed

2 files changed

+14
-23
lines changed

app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php

+8-22
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace Magento\QuoteGraphQl\Model\Cart;
99

1010
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
11-
use Magento\Framework\Message\AbstractMessage;
11+
use Magento\Framework\Message\MessageInterface;
1212
use Magento\Quote\Api\CartRepositoryInterface;
1313
use Magento\Quote\Model\Quote;
1414

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

5757
if ($cart->getData('has_error')) {
58-
throw new GraphQlInputException(
59-
__('Shopping cart error: %message', ['message' => $this->getCartErrors($cart)])
60-
);
58+
$e = new GraphQlInputException(__('Shopping cart errors'));
59+
$errors = $cart->getErrors();
60+
foreach ($errors as $error) {
61+
/** @var MessageInterface $error */
62+
$e->addError(new GraphQlInputException(__($error->getText())));
63+
}
64+
throw $e;
6165
}
6266

6367
$this->cartRepository->save($cart);
6468
}
65-
66-
/**
67-
* Collecting cart errors
68-
*
69-
* @param Quote $cart
70-
* @return string
71-
*/
72-
private function getCartErrors(Quote $cart): string
73-
{
74-
$errorMessages = [];
75-
76-
/** @var AbstractMessage $error */
77-
foreach ($cart->getErrors() as $error) {
78-
$errorMessages[] = $error->getText();
79-
}
80-
81-
return implode(PHP_EOL, $errorMessages);
82-
}
8369
}

app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,12 @@ public function execute(Quote $cart, array $cartItemData): void
7272
}
7373

7474
if (is_string($result)) {
75-
throw new GraphQlInputException(__($result));
75+
$e = new GraphQlInputException(__('Cannot add product to cart'));
76+
$errors = array_unique(explode("\n", $result));
77+
foreach ($errors as $error) {
78+
$e->addError(new GraphQlInputException(__($error)));
79+
}
80+
throw $e;
7681
}
7782
}
7883

0 commit comments

Comments
 (0)