Skip to content

Commit be68e78

Browse files
author
Serhiy Shkolyarenko
committed
MAGETWO-39117: Updating qty that exceeds the qty in stock for registered customer by API should return error
added separate multimessage exception
1 parent 8e7868d commit be68e78

File tree

2 files changed

+45
-20
lines changed

2 files changed

+45
-20
lines changed

app/code/Magento/Quote/Model/QuoteRepository/SaveHandler.php

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
use Magento\Quote\Api\Data\CartInterface;
99
use Magento\Framework\Exception\InputException;
10-
use Magento\Framework\Exception\CouldNotSaveException;
10+
use Magento\Framework\Exception\CouldNotSaveMultiMessageException;
1111

1212
class SaveHandler
1313
{
@@ -73,10 +73,7 @@ public function save(CartInterface $quote)
7373
}
7474
$errors = $quote->getErrors();
7575
if (!empty($errors)) {
76-
$renderedErrors = $this->renderErrors($errors);
77-
throw new CouldNotSaveException(
78-
__('Following errors occurred on save: %1', implode('; ', $renderedErrors))
79-
);
76+
throw new CouldNotSaveMultiMessageException('Following errors occurred on save: %1', $errors);
8077
}
8178

8279
// Billing Address processing
@@ -91,21 +88,6 @@ public function save(CartInterface $quote)
9188
return $quote;
9289
}
9390

94-
/**
95-
* @param array $errors
96-
* @return array
97-
*/
98-
private function renderErrors($errors)
99-
{
100-
$renderedErrors = [];
101-
/** @var \Magento\Framework\Message\Error $error */
102-
foreach ($errors as $error) {
103-
$renderedErrors[] = $error->getText();
104-
}
105-
return $renderedErrors;
106-
}
107-
108-
10991
/**
11092
* @param \Magento\Quote\Model\Quote $quote
11193
* @return void
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Framework\Exception;
8+
9+
use Magento\Framework\Phrase;
10+
11+
class CouldNotSaveMultiMessageException extends CouldNotSaveException
12+
{
13+
/**
14+
* @param string $mainMessage
15+
* @param \Magento\Framework\Message\Error[] $errors
16+
* @param string $separator
17+
*/
18+
public function __construct($mainMessage, $errors, $separator = '; ')
19+
{
20+
$renderedErrors = $this->renderErrors($errors, $separator);
21+
// $mainMessage should contain %1 to be substituted by concatenated errors
22+
$phrase = new \Magento\Framework\Phrase($mainMessage, [$renderedErrors]);
23+
parent::__construct($phrase);
24+
}
25+
26+
/**
27+
* @param \Magento\Framework\Message\Error[] $errors
28+
* @param string $separator
29+
* @return string
30+
*/
31+
private function renderErrors($errors, $separator)
32+
{
33+
$renderedErrors = '';
34+
$eol = '';
35+
/** @var \Magento\Framework\Message\Error $error */
36+
foreach ($errors as $error) {
37+
$renderedErrors .= $eol . __($error->getText())->render();
38+
$eol = $separator;
39+
}
40+
return $renderedErrors;
41+
}
42+
43+
}

0 commit comments

Comments
 (0)