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

GraphQl-416: graphql-input provides to Customer as many errors as appeared instead of last one like on Magento Storefront #421

Closed
wants to merge 11 commits into from
Closed
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
6 changes: 5 additions & 1 deletion app/code/Magento/Catalog/Model/Product/Type/AbstractType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
{
Expand All @@ -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 {
Expand All @@ -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;
}

Expand All @@ -603,7 +607,7 @@ protected function _prepareOptions(\Magento\Framework\DataObject $buyRequest, $p
}
}
if (count($results) > 0) {
throw new LocalizedException(__(implode("\n", $results)));
throw InvalidOptionInput::getExtendedMessage($results, $extendedData);
}
}

Expand Down
61 changes: 61 additions & 0 deletions lib/internal/Magento/Framework/Exception/InvalidOptionInput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* InvalidOptionInput Exception
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Framework\Exception;

use Magento\Framework\Phrase;

/**
* InvalidOptionInput exception
*
* @api
*/
class InvalidOptionInput extends AbstractAggregateException implements AggregateExceptionInterface
{
/**
* InvalidOptionInput constructor.
*
* @param \Magento\Framework\Phrase $phrase
* @param \Exception|null $cause
* @param int $code
*/
public function __construct(Phrase $phrase = null, \Exception $cause = null, int $code = 0)
{
if ($phrase === null) {
$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);
}

/**
* Get extended message
*
* @param array $messages
* @param array $extendedData
* @param \Exception|null $cause
*
* @return \Magento\Framework\Exception\InvalidOptionInput
*/
public static function getExtendedMessage(array $messages, array $extendedData, \Exception $cause = null)
{
foreach ($messages as $key => &$value) {
// @codingStandardsIgnoreLine
$value .= " Please check input data: Product SKU - '{$extendedData[$key]['sku']}', Option ID - '{$extendedData[$key]['optionId']}'";
}

return new self(
new Phrase(
__(implode("\n", $messages))
),
$cause
);
}
}