From 08d33c81e685edf96754bd536a3aeab9579fe155 Mon Sep 17 00:00:00 2001 From: Ievgenii Gryshkun Date: Mon, 4 Mar 2019 12:45:11 +0200 Subject: [PATCH 1/9] graphql-input provides to Customer as many errors as appeared instead of last one like on Magento Storefront --- .../Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php b/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php index 1b32866ed883..e2921b4205e3 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php @@ -87,7 +87,8 @@ public function execute(Quote $cart, array $cartItemData): void } if (is_string($result)) { - throw new GraphQlInputException(__($result)); + $uniqueMessqges = array_unique(explode("\n", $result)); + throw new GraphQlInputException(__(implode("\n", $uniqueMessqges))); } } From 1f8c1f301f0e3083bc5d64cbdde021876995e381 Mon Sep 17 00:00:00 2001 From: Ievgenii Gryshkun Date: Tue, 5 Mar 2019 12:26:44 +0200 Subject: [PATCH 2/9] graphql-input provides to Customer as many errors as appeared instead of last one like on Magento Storefront --- app/code/Magento/Catalog/Model/Product/Type/AbstractType.php | 2 +- .../Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/Catalog/Model/Product/Type/AbstractType.php b/app/code/Magento/Catalog/Model/Product/Type/AbstractType.php index e6804d9246fa..7321a95c7aa6 100644 --- a/app/code/Magento/Catalog/Model/Product/Type/AbstractType.php +++ b/app/code/Magento/Catalog/Model/Product/Type/AbstractType.php @@ -603,7 +603,7 @@ protected function _prepareOptions(\Magento\Framework\DataObject $buyRequest, $p } } if (count($results) > 0) { - throw new LocalizedException(__(implode("\n", $results))); + throw new LocalizedException(__(implode("\n", array_unique($results)))); } } diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php b/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php index e2921b4205e3..1b32866ed883 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php @@ -87,8 +87,7 @@ public function execute(Quote $cart, array $cartItemData): void } if (is_string($result)) { - $uniqueMessqges = array_unique(explode("\n", $result)); - throw new GraphQlInputException(__(implode("\n", $uniqueMessqges))); + throw new GraphQlInputException(__($result)); } } From 613df67a088bcd9877925d09eaed5e2a1cd476a4 Mon Sep 17 00:00:00 2001 From: Ievgenii Gryshkun Date: Tue, 9 Jul 2019 12:09:09 +0300 Subject: [PATCH 3/9] graphql-input provides to Customer as many errors as appeared instead of last one like on Magento Storefront --- .../Model/Product/Type/AbstractType.php | 8 ++- .../Exception/InvalidOptionInput.php | 53 +++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 lib/internal/Magento/Framework/Exception/InvalidOptionInput.php diff --git a/app/code/Magento/Catalog/Model/Product/Type/AbstractType.php b/app/code/Magento/Catalog/Model/Product/Type/AbstractType.php index 7321a95c7aa6..e95d40271b12 100644 --- a/app/code/Magento/Catalog/Model/Product/Type/AbstractType.php +++ b/app/code/Magento/Catalog/Model/Product/Type/AbstractType.php @@ -9,6 +9,7 @@ use Magento\Catalog\Api\ProductRepositoryInterface; use Magento\Framework\App\Filesystem\DirectoryList; use Magento\Framework\Exception\LocalizedException; +use Magento\Framework\Exception\InvalidOptionInput; /** * @api @@ -572,6 +573,7 @@ public function getSpecifyOptionMessage() * @param string $processMode * @return array * @throws LocalizedException + * @throws InvalidOptionInput */ protected function _prepareOptions(\Magento\Framework\DataObject $buyRequest, $product, $processMode) { @@ -583,6 +585,7 @@ protected function _prepareOptions(\Magento\Framework\DataObject $buyRequest, $p } if ($options !== null) { $results = []; + $extendedData = []; foreach ($options as $option) { /* @var $option \Magento\Catalog\Model\Product\Option */ try { @@ -594,6 +597,7 @@ protected function _prepareOptions(\Magento\Framework\DataObject $buyRequest, $p ->validateUserValue($buyRequest->getOptions()); } catch (LocalizedException $e) { $results[] = $e->getMessage(); + $extendedData[] = ['sku' => $product->getSku(), 'optionId' => $option->getId()]; continue; } @@ -603,7 +607,9 @@ protected function _prepareOptions(\Magento\Framework\DataObject $buyRequest, $p } } if (count($results) > 0) { - throw new LocalizedException(__(implode("\n", array_unique($results)))); + throw new InvalidOptionInput( + new \Magento\Framework\Phrase(__(implode("\n", $results)), $extendedData) + ); } } diff --git a/lib/internal/Magento/Framework/Exception/InvalidOptionInput.php b/lib/internal/Magento/Framework/Exception/InvalidOptionInput.php new file mode 100644 index 000000000000..0aa26757b5a7 --- /dev/null +++ b/lib/internal/Magento/Framework/Exception/InvalidOptionInput.php @@ -0,0 +1,53 @@ +getArguments(); + if (isset($arguments)) { + $this->phrase = $phrase; + $phrase = new Phrase($this->getExtendedMessage()); + } + parent::__construct($phrase, $cause, $code); + } + + /** + * Add more information to exception message for custom option of product + * + * @return string + */ + public function getExtendedMessage() + { + $exceptionTextArray = explode("\n", $this->phrase->getText()); + $extendedData = $this->phrase->getArguments(); + foreach ($exceptionTextArray as $key => $value) { + $exceptionTextArray[$key] .= + " Please check input data: Product SKU - '{$extendedData[$key]['sku']}', Option ID - '{$extendedData[$key]['optionId']}'"; + } + + return __(implode("\n", $exceptionTextArray)); + } +} From 6ee55a301b8a4a3c452df5005cb92b1c25e35941 Mon Sep 17 00:00:00 2001 From: Ievgenii Gryshkun Date: Tue, 9 Jul 2019 14:33:17 +0300 Subject: [PATCH 4/9] graphql-input provides to Customer as many errors as appeared instead of last one like on Magento Storefront --- .../Magento/Framework/Exception/InvalidOptionInput.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/internal/Magento/Framework/Exception/InvalidOptionInput.php b/lib/internal/Magento/Framework/Exception/InvalidOptionInput.php index 0aa26757b5a7..f885a25c02fa 100644 --- a/lib/internal/Magento/Framework/Exception/InvalidOptionInput.php +++ b/lib/internal/Magento/Framework/Exception/InvalidOptionInput.php @@ -43,9 +43,9 @@ public function getExtendedMessage() { $exceptionTextArray = explode("\n", $this->phrase->getText()); $extendedData = $this->phrase->getArguments(); - foreach ($exceptionTextArray as $key => $value) { - $exceptionTextArray[$key] .= - " Please check input data: Product SKU - '{$extendedData[$key]['sku']}', Option ID - '{$extendedData[$key]['optionId']}'"; + foreach ($exceptionTextArray as $key => &$value) { + // @codingStandardsIgnoreLine + $value .= " Please check input data: Product SKU - '{$extendedData[$key]['sku']}', Option ID - '{$extendedData[$key]['optionId']}'"; } return __(implode("\n", $exceptionTextArray)); From 79da9fd04b3c4e29c47144dff616b353ed5e92b9 Mon Sep 17 00:00:00 2001 From: Ievgenii Gryshkun Date: Thu, 18 Jul 2019 12:12:02 +0300 Subject: [PATCH 5/9] GraphQl-416: graphql-input provides to Customer as many errors as appeared instead of last one like on Magento Storefront --- .../Model/Product/Type/AbstractType.php | 4 +-- .../Exception/InvalidOptionInput.php | 33 ++++++++++++------- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/app/code/Magento/Catalog/Model/Product/Type/AbstractType.php b/app/code/Magento/Catalog/Model/Product/Type/AbstractType.php index e95d40271b12..0e94cd00cee8 100644 --- a/app/code/Magento/Catalog/Model/Product/Type/AbstractType.php +++ b/app/code/Magento/Catalog/Model/Product/Type/AbstractType.php @@ -607,9 +607,7 @@ protected function _prepareOptions(\Magento\Framework\DataObject $buyRequest, $p } } if (count($results) > 0) { - throw new InvalidOptionInput( - new \Magento\Framework\Phrase(__(implode("\n", $results)), $extendedData) - ); + throw InvalidOptionInput::getExtendedMessage($results, $extendedData); } } diff --git a/lib/internal/Magento/Framework/Exception/InvalidOptionInput.php b/lib/internal/Magento/Framework/Exception/InvalidOptionInput.php index f885a25c02fa..b185ec76ee2a 100644 --- a/lib/internal/Magento/Framework/Exception/InvalidOptionInput.php +++ b/lib/internal/Magento/Framework/Exception/InvalidOptionInput.php @@ -17,6 +17,12 @@ */ class InvalidOptionInput extends LocalizedException { + /** + * Default message + */ + private const DEFAULT_MESSAGE = + 'The product\'s required option(s) weren\'t entered. Make sure the options are entered and try again.'; + /** * InvalidOptionInput constructor. * @@ -24,30 +30,33 @@ class InvalidOptionInput extends LocalizedException * @param \Exception|null $cause * @param int $code */ - public function __construct(\Magento\Framework\Phrase $phrase, \Exception $cause = null, int $code = 0) + public function __construct(Phrase $phrase = null, \Exception $cause = null, int $code = 0) { - $arguments = $phrase->getArguments(); - if (isset($arguments)) { - $this->phrase = $phrase; - $phrase = new Phrase($this->getExtendedMessage()); + if ($phrase === null) { + $phrase = new Phrase(__(self::DEFAULT_MESSAGE)); } parent::__construct($phrase, $cause, $code); } /** - * Add more information to exception message for custom option of product + * @param array $messages + * @param array $extendedData + * @param \Exception|null $cause * - * @return string + * @return \Magento\Framework\Exception\InvalidOptionInput */ - public function getExtendedMessage() + public static function getExtendedMessage(array $messages, array $extendedData, \Exception $cause = null) { - $exceptionTextArray = explode("\n", $this->phrase->getText()); - $extendedData = $this->phrase->getArguments(); - foreach ($exceptionTextArray as $key => &$value) { + foreach ($messages as $key => &$value) { // @codingStandardsIgnoreLine $value .= " Please check input data: Product SKU - '{$extendedData[$key]['sku']}', Option ID - '{$extendedData[$key]['optionId']}'"; } - return __(implode("\n", $exceptionTextArray)); + return new self( + new Phrase( + __(implode("\n", $messages)) + ), + $cause + ); } } From 270e3b955ca60b1c101fc159d93fd631ad4aa162 Mon Sep 17 00:00:00 2001 From: Ievgenii Gryshkun Date: Thu, 18 Jul 2019 13:01:48 +0300 Subject: [PATCH 6/9] GraphQl-416: graphql-input provides to Customer as many errors as appeared instead of last one like on Magento Storefront --- lib/internal/Magento/Framework/Exception/InvalidOptionInput.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/internal/Magento/Framework/Exception/InvalidOptionInput.php b/lib/internal/Magento/Framework/Exception/InvalidOptionInput.php index b185ec76ee2a..b0fd42dbacd6 100644 --- a/lib/internal/Magento/Framework/Exception/InvalidOptionInput.php +++ b/lib/internal/Magento/Framework/Exception/InvalidOptionInput.php @@ -5,6 +5,7 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ +declare(strict_types=1); namespace Magento\Framework\Exception; From a866b07b43008cfbf87e3984555b2f6926881fad Mon Sep 17 00:00:00 2001 From: Ievgenii Gryshkun Date: Thu, 1 Aug 2019 09:59:57 +0300 Subject: [PATCH 7/9] graphql-input provides to Customer as many errors as appeared instead of last one like on Magento Storefront --- .../Magento/Framework/Exception/InvalidOptionInput.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/internal/Magento/Framework/Exception/InvalidOptionInput.php b/lib/internal/Magento/Framework/Exception/InvalidOptionInput.php index b0fd42dbacd6..1c536d259b56 100644 --- a/lib/internal/Magento/Framework/Exception/InvalidOptionInput.php +++ b/lib/internal/Magento/Framework/Exception/InvalidOptionInput.php @@ -34,12 +34,14 @@ class InvalidOptionInput extends LocalizedException public function __construct(Phrase $phrase = null, \Exception $cause = null, int $code = 0) { if ($phrase === null) { - $phrase = new Phrase(__(self::DEFAULT_MESSAGE)); + $phrase = new Phrase(self::DEFAULT_MESSAGE); } parent::__construct($phrase, $cause, $code); } /** + * Get extended message + * * @param array $messages * @param array $extendedData * @param \Exception|null $cause From 146ce00225be3fb67d4c4e8be3c98804b835a6a9 Mon Sep 17 00:00:00 2001 From: Ievgenii Gryshkun Date: Thu, 1 Aug 2019 12:12:47 +0300 Subject: [PATCH 8/9] graphql-input provides to Customer as many errors as appeared instead of last one like on Magento Storefront --- .../Magento/Framework/Exception/InvalidOptionInput.php | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/lib/internal/Magento/Framework/Exception/InvalidOptionInput.php b/lib/internal/Magento/Framework/Exception/InvalidOptionInput.php index 1c536d259b56..903b96babbd0 100644 --- a/lib/internal/Magento/Framework/Exception/InvalidOptionInput.php +++ b/lib/internal/Magento/Framework/Exception/InvalidOptionInput.php @@ -18,12 +18,6 @@ */ class InvalidOptionInput extends LocalizedException { - /** - * Default message - */ - private const DEFAULT_MESSAGE = - 'The product\'s required option(s) weren\'t entered. Make sure the options are entered and try again.'; - /** * InvalidOptionInput constructor. * @@ -34,7 +28,9 @@ class InvalidOptionInput extends LocalizedException public function __construct(Phrase $phrase = null, \Exception $cause = null, int $code = 0) { if ($phrase === null) { - $phrase = new Phrase(self::DEFAULT_MESSAGE); + $phrase = new Phrase( + 'The product\'s required option(s) weren\'t entered. Make sure the options are entered and try again.' + ); } parent::__construct($phrase, $cause, $code); } From 61e68be371b7ebbcd35990efa0086934a91226fe Mon Sep 17 00:00:00 2001 From: Ievgenii Gryshkun Date: Wed, 4 Sep 2019 13:41:38 +0300 Subject: [PATCH 9/9] GraphQl-416: graphql-input provides to Customer as many errors as appeared instead of last one like on Magento Storefront #421 --- lib/internal/Magento/Framework/Exception/InvalidOptionInput.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/Magento/Framework/Exception/InvalidOptionInput.php b/lib/internal/Magento/Framework/Exception/InvalidOptionInput.php index 903b96babbd0..064764b561ef 100644 --- a/lib/internal/Magento/Framework/Exception/InvalidOptionInput.php +++ b/lib/internal/Magento/Framework/Exception/InvalidOptionInput.php @@ -16,7 +16,7 @@ * * @api */ -class InvalidOptionInput extends LocalizedException +class InvalidOptionInput extends AbstractAggregateException implements AggregateExceptionInterface { /** * InvalidOptionInput constructor.