Skip to content

Commit 8352914

Browse files
author
Dmytro Poperechnyy
committed
MAGETWO-33814: Change parent class of Model\Exception sub-classes to Localized
- Error code eliminated in app/code and in tests; - Added AlreadyExistsException to library;
1 parent 5608e8e commit 8352914

File tree

6 files changed

+47
-38
lines changed

6 files changed

+47
-38
lines changed

app/code/Magento/Tax/Model/Calculation/RateRepository.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
use Magento\Framework\Api\SearchCriteria;
1414
use Magento\Framework\Api\SortOrder;
1515
use Magento\Framework\Exception\InputException;
16-
use Magento\Framework\Model\Exception as ModelException;
16+
use Magento\Framework\Exception\LocalizedException;
17+
use Magento\Framework\Exception\AlreadyExistsException;
1718
use Magento\Tax\Api\Data\TaxRateInterface as TaxRateDataObject;
1819
use Magento\Tax\Model\Calculation\Rate\Converter;
1920
use Magento\Tax\Model\Resource\Calculation\Rate\Collection;
@@ -104,12 +105,10 @@ public function save(\Magento\Tax\Api\Data\TaxRateInterface $taxRate)
104105
try {
105106
$this->resourceModel->save($taxRate);
106107
$taxRate->saveTitles($taxRateTitles);
107-
} catch (ModelException $e) {
108-
if ($e->getCode() == ModelException::ERROR_CODE_ENTITY_ALREADY_EXISTS) {
109-
throw new InputException($e->getMessage());
110-
} else {
111-
throw $e;
112-
}
108+
} catch (AlreadyExistsException $e) {
109+
throw new InputException($e->getMessage());
110+
} catch (LocalizedException $e) {
111+
throw $e;
113112
}
114113
$this->rateRegistry->registerTaxRate($taxRate);
115114
return $taxRate;

app/code/Magento/Tax/Model/TaxRuleRepository.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99
use Magento\Framework\Api\Search\FilterGroup;
1010
use Magento\Framework\Api\SearchCriteria;
1111
use Magento\Framework\Api\SortOrder;
12-
use Magento\Framework\Exception\CouldNotSaveException;
1312
use Magento\Framework\Exception\InputException;
13+
use Magento\Framework\Exception\LocalizedException;
14+
use Magento\Framework\Exception\CouldNotSaveException;
1415
use Magento\Framework\Exception\NoSuchEntityException;
15-
use Magento\Framework\Model\Exception as ModelException;
16+
use Magento\Framework\Exception\AlreadyExistsException;
1617
use Magento\Tax\Api\Data\TaxRuleInterface;
17-
use Magento\Tax\Api\Data\TaxRuleSearchResultsDataBuilder;
1818
use Magento\Tax\Api\TaxRuleRepositoryInterface;
19+
use Magento\Tax\Api\Data\TaxRuleSearchResultsDataBuilder;
1920
use Magento\Tax\Model\Calculation\RuleFactory;
2021
use Magento\Tax\Model\Calculation\TaxRuleRegistry;
2122
use Magento\Tax\Model\Resource\Calculation\Rule as Resource;
@@ -92,14 +93,12 @@ public function save(TaxRuleInterface $rule)
9293
$this->taxRuleRegistry->retrieveTaxRule($ruleId);
9394
}
9495
$this->resource->save($rule);
95-
} catch (ModelException $e) {
96-
if ($e->getCode() == ModelException::ERROR_CODE_ENTITY_ALREADY_EXISTS) {
97-
throw new InputException($e->getMessage());
98-
} else {
99-
throw new CouldNotSaveException($e->getMessage());
100-
}
101-
} catch (NoSuchEntityException $exception) {
102-
throw $exception;
96+
} catch (AlreadyExistsException $e) {
97+
throw new InputException($e->getMessage());
98+
} catch (NoSuchEntityException $e) {
99+
throw $e;
100+
} catch (LocalizedException $e) {
101+
throw new CouldNotSaveException($e->getMessage());
103102
}
104103
$this->taxRuleRegistry->registerTaxRule($rule);
105104
return $rule;

dev/tests/unit/testsuite/Magento/Tax/Model/Calculation/RateRepositoryTest.php

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
*/
66
namespace Magento\Tax\Model\Calculation;
77

8-
use Magento\TestFramework\Helper\ObjectManager;
9-
use Magento\Framework\Model\Exception as ModelException;
108
use Magento\Framework\Api\SearchCriteria;
9+
use Magento\TestFramework\Helper\ObjectManager;
10+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\Framework\Exception\AlreadyExistsException;
1112

1213
class RateRepositoryTest extends \PHPUnit_Framework_TestCase
1314
{
@@ -264,13 +265,13 @@ private function getTaxRateMock(array $taxRateData)
264265
}
265266

266267
/**
267-
* @param ModelException $expectedException
268+
* @dataProvider saveThrowsExceptionIfCannotSaveTitlesDataProvider
269+
* @param LocalizedException $expectedException
268270
* @param string $exceptionType
269271
* @param string $exceptionMessage
270-
* @throws ModelException
272+
* @throws LocalizedException
271273
* @throws \Exception
272274
* @throws \Magento\Framework\Exception\InputException
273-
* @dataProvider saveThrowsExceptionIfCannotSaveTitlesDataProvider
274275
*/
275276
public function testSaveThrowsExceptionIfCannotSaveTitles($expectedException, $exceptionType, $exceptionMessage)
276277
{
@@ -319,18 +320,13 @@ public function saveThrowsExceptionIfCannotSaveTitlesDataProvider()
319320
{
320321
return [
321322
'entity_already_exists' => [
322-
new ModelException(
323-
'Cannot save titles',
324-
ModelException::ERROR_CODE_ENTITY_ALREADY_EXISTS
325-
),
323+
new AlreadyExistsException('Entity already exists'),
326324
'Magento\Framework\Exception\InputException',
327-
'Cannot save titles'
325+
'Entity already exists'
328326
],
329327
'cannot_save_title' => [
330-
new ModelException(
331-
'Cannot save titles'
332-
),
333-
'Magento\Framework\Model\Exception',
328+
new LocalizedException('Cannot save titles'),
329+
'Magento\Framework\Exception\LocalizedException',
334330
'Cannot save titles'
335331
]
336332
];

dev/tests/unit/testsuite/Magento/Tax/Model/TaxRuleRepositoryTest.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66
namespace Magento\Tax\Model;
77

8-
use Magento\Framework\Model\Exception as ModelException;
98
use Magento\Framework\Api\SearchCriteria as SearchCriteria;
109

1110
class TaxRuleRepositoryTest extends \PHPUnit_Framework_TestCase
@@ -118,6 +117,10 @@ public function testSave()
118117
* @param $exceptionObject
119118
* @param $exceptionName
120119
* @param $exceptionMessage
120+
* @throws \Exception
121+
* @throws \Magento\Framework\Exception\CouldNotSaveException
122+
* @throws \Magento\Framework\Exception\InputException
123+
* @throws \Magento\Framework\Exception\NoSuchEntityException
121124
*/
122125
public function testSaveWithExceptions($exceptionObject, $exceptionName, $exceptionMessage)
123126
{
@@ -137,13 +140,13 @@ public function saveExceptionsDataProvider()
137140
{
138141
return [
139142
[
140-
new \Magento\Framework\Model\Exception('Could not save'),
143+
new \Magento\Framework\Exception\LocalizedException('Could not save'),
141144
'\Magento\Framework\Exception\CouldNotSaveException',
142145
'Could not save'
143146
], [
144-
new \Magento\Framework\Model\Exception('InputError', ModelException::ERROR_CODE_ENTITY_ALREADY_EXISTS),
147+
new \Magento\Framework\Exception\AlreadyExistsException('Entity already exists'),
145148
'\Magento\Framework\Exception\InputException',
146-
'InputError'
149+
'Entity already exists'
147150
], [
148151
new \Magento\Framework\Exception\NoSuchEntityException('No such entity'),
149152
'\Magento\Framework\Exception\NoSuchEntityException',
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Framework\Exception;
8+
9+
class AlreadyExistsException extends \Magento\Framework\Exception\LocalizedException
10+
{
11+
}

lib/internal/Magento/Framework/Model/Resource/Db/AbstractDb.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
namespace Magento\Framework\Model\Resource\Db;
88

9+
use Magento\Framework\Exception\AlreadyExistsException;
910
use Magento\Framework\Model\Exception as ModelException;
1011

1112
/**
@@ -596,7 +597,7 @@ protected function _prepareValueForSave($value, $type)
596597
*
597598
* @param \Magento\Framework\Model\AbstractModel $object
598599
* @return $this
599-
* @throws ModelException
600+
* @throws AlreadyExistsException
600601
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
601602
*/
602603
protected function _checkUnique(\Magento\Framework\Model\AbstractModel $object)
@@ -639,7 +640,7 @@ protected function _checkUnique(\Magento\Framework\Model\AbstractModel $object)
639640
} else {
640641
$error = __('%1 already exist.', implode(', ', $existent));
641642
}
642-
throw new ModelException($error, ModelException::ERROR_CODE_ENTITY_ALREADY_EXISTS);
643+
throw new AlreadyExistsException($error);
643644
}
644645
return $this;
645646
}

0 commit comments

Comments
 (0)