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

Commit

Permalink
#416: graphql-input provides to Customer as many errors as appeared i…
Browse files Browse the repository at this point in the history
…nstead of last one like on Magento Storefront
  • Loading branch information
lenaorobei committed Nov 8, 2019
1 parent 4a89312 commit cbeb99c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ protected function _prepareOptions(\Magento\Framework\DataObject $buyRequest, $p
}
}
if (count($results) > 0) {
throw new LocalizedException(__(implode("\n", array_unique($results))));
throw new LocalizedException(__(implode("\n", $results)));
}
}

Expand Down
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

0 comments on commit cbeb99c

Please sign in to comment.