Skip to content

Commit cbeb99c

Browse files
committed
magento/graphql-ce#416: graphql-input provides to Customer as many errors as appeared instead of last one like on Magento Storefront
1 parent 4a89312 commit cbeb99c

File tree

3 files changed

+15
-24
lines changed

3 files changed

+15
-24
lines changed

app/code/Magento/Catalog/Model/Product/Type/AbstractType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ protected function _prepareOptions(\Magento\Framework\DataObject $buyRequest, $p
603603
}
604604
}
605605
if (count($results) > 0) {
606-
throw new LocalizedException(__(implode("\n", array_unique($results))));
606+
throw new LocalizedException(__(implode("\n", $results)));
607607
}
608608
}
609609

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

Lines changed: 8 additions & 22 deletions
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

Lines changed: 6 additions & 1 deletion
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)