Skip to content

Commit 17c761b

Browse files
MAGETWO-67722: Add ability to inject exception code in LocalizedException #9363
2 parents 76ebd3e + 3a5862e commit 17c761b

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,14 @@ class LocalizedException extends \Exception
2626
protected $logMessage;
2727

2828
/**
29-
* Constructor
30-
*
3129
* @param \Magento\Framework\Phrase $phrase
3230
* @param \Exception $cause
31+
* @param int $code
3332
*/
34-
public function __construct(Phrase $phrase, \Exception $cause = null)
33+
public function __construct(Phrase $phrase, \Exception $cause = null, $code = 0)
3534
{
3635
$this->phrase = $phrase;
37-
parent::__construct($phrase->render(), 0, $cause);
36+
parent::__construct($phrase->render(), intval($code), $cause);
3837
}
3938

4039
/**

lib/internal/Magento/Framework/Exception/Test/Unit/LocalizedExceptionTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,4 +150,16 @@ public function testGetLogMessage()
150150
$expectedLogMessage = 'message parameter1 parameter2';
151151
$this->assertEquals($expectedLogMessage, $localizeException->getLogMessage());
152152
}
153+
154+
public function testGetCode()
155+
{
156+
$expectedCode = 42;
157+
$localizedException = new LocalizedException(
158+
new Phrase("message %1", ['test']),
159+
new \Exception(),
160+
$expectedCode
161+
);
162+
163+
$this->assertEquals($expectedCode, $localizedException->getCode());
164+
}
153165
}

0 commit comments

Comments
 (0)