Skip to content

Commit 6896a7a

Browse files
authored
Update AbstractAggregateException.php
1 parent e4d64de commit 6896a7a

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed

lib/internal/Magento/Framework/Exception/AbstractAggregateException.php

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,90 @@ public function addError(Phrase $phrase)
8282
* @param LocalizedException $exception
8383
* @return $this
8484
*/
85+
public function addException(LocalizedException $exception)<?php
86+
/**
87+
* Copyright © Magento, Inc. All rights reserved.
88+
* See COPYING.txt for license details.
89+
*/
90+
91+
namespace Magento\Framework\Exception;
92+
93+
use Magento\Framework\Phrase;
94+
95+
/**
96+
* @api
97+
*/
98+
abstract class AbstractAggregateException extends LocalizedException
99+
{
100+
/**
101+
* The array of errors that have been added via the addError() method
102+
*
103+
* @var \Magento\Framework\Exception\LocalizedException[]
104+
*/
105+
protected $errors = [];
106+
107+
/**
108+
* The original phrase
109+
*
110+
* @var \Magento\Framework\Phrase
111+
*/
112+
protected $originalPhrase;
113+
114+
/**
115+
* An internal variable indicating how many time addError has been called
116+
*
117+
* @var int
118+
*/
119+
private $addErrorCalls = 0;
120+
121+
/**
122+
* Initialize the exception
123+
*
124+
* @param \Magento\Framework\Phrase $phrase
125+
* @param \Exception $cause
126+
* @param int $code
127+
*/
128+
public function __construct(Phrase $phrase, \Exception $cause = null, $code = 0)
129+
{
130+
$this->originalPhrase = $phrase;
131+
parent::__construct($phrase, $cause, $code);
132+
}
133+
134+
/**
135+
* Add new error into the list of exceptions
136+
*
137+
* @param \Magento\Framework\Phrase $phrase
138+
* @return $this
139+
*/
140+
public function addError(Phrase $phrase)
141+
{
142+
$this->addErrorCalls++;
143+
if (empty($this->errors)) {
144+
if (1 === $this->addErrorCalls) {
145+
// First call: simply overwrite the phrase and message
146+
$this->phrase = $phrase;
147+
$this->message = $phrase->render();
148+
$this->logMessage = null;
149+
} elseif (2 === $this->addErrorCalls) {
150+
// Second call: store the error from the first call and the second call in the array
151+
// restore the phrase to its original value
152+
$this->errors[] = new LocalizedException($this->phrase);
153+
$this->errors[] = new LocalizedException($phrase);
154+
$this->phrase = $this->originalPhrase;
155+
$this->message = $this->originalPhrase->render();
156+
$this->logMessage = null;
157+
}
158+
} else {
159+
// All subsequent calls after the second should reach here
160+
$this->errors[] = new LocalizedException($phrase);
161+
}
162+
return $this;
163+
}
164+
165+
/**
166+
* @param LocalizedException $exception
167+
* @return $this
168+
*/
85169
public function addException(LocalizedException $exception)
86170
{
87171
$this->addErrorCalls++;
@@ -109,3 +193,30 @@ public function getErrors()
109193
return $this->errors;
110194
}
111195
}
196+
197+
{
198+
$this->addErrorCalls++;
199+
$this->errors[] = $exception;
200+
return $this;
201+
}
202+
203+
/**
204+
* Should return true if someone has added different errors to this exception after construction
205+
*
206+
* @return bool
207+
*/
208+
public function wasErrorAdded()
209+
{
210+
return (0 < $this->addErrorCalls);
211+
}
212+
213+
/**
214+
* Get the array of LocalizedException objects. Get an empty array if no errors were added.
215+
*
216+
* @return \Magento\Framework\Exception\LocalizedException[]
217+
*/
218+
public function getErrors()
219+
{
220+
return $this->errors;
221+
}
222+
}

0 commit comments

Comments
 (0)