Skip to content

Commit c95e4cb

Browse files
committed
MAGETWO-56260: Recoverable Exception Support
1 parent da6a294 commit c95e4cb

File tree

5 files changed

+62
-7
lines changed

5 files changed

+62
-7
lines changed

app/code/Magento/Catalog/Model/Product/TierPriceManagement.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use Magento\Customer\Api\GroupRepositoryInterface;
1313
use Magento\Framework\Exception\CouldNotSaveException;
1414
use Magento\Framework\Exception\InputException;
15-
use Magento\Framework\Exception\TemporaryStateException;
15+
use Magento\Framework\Exception\TemporaryStateExceptionInterface;
1616

1717
/**
1818
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -138,7 +138,7 @@ public function add($sku, $customerGroupId, $price, $qty)
138138
try {
139139
$this->productRepository->save($product);
140140
} catch (\Exception $e) {
141-
if ($e instanceof TemporaryStateException) {
141+
if ($e instanceof TemporaryStateExceptionInterface) {
142142
// temporary state exception must be already localized
143143
throw $e;
144144
}

app/code/Magento/Catalog/Test/Unit/Model/Product/TierPriceManagementTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
use Magento\Customer\Model\GroupManagement;
1414
use Magento\Framework\Exception\NoSuchEntityException;
15+
use Magento\Framework\Exception\TemporaryState\CouldNotSaveException;
1516

1617
/**
1718
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -393,6 +394,30 @@ public function testSetThrowsExceptionIfCantSave()
393394
$this->service->add('product_sku', 1, 100, 2);
394395
}
395396

397+
/**
398+
* @expectedException \Magento\Framework\Exception\TemporaryState\CouldNotSaveException
399+
*/
400+
public function testAddRethrowsTemporaryStateExceptionIfRecoverableErrorOccurred()
401+
{
402+
$group = $this->getMock(\Magento\Customer\Model\Data\Group::class, [], [], '', false);
403+
$group->expects($this->once())
404+
->method('getId')
405+
->willReturn(1);
406+
$this->productMock
407+
->expects($this->once())
408+
->method('getData')
409+
->with('tier_price')
410+
->will($this->returnValue([]));
411+
$this->groupRepositoryMock->expects($this->once())
412+
->method('getById')
413+
->willReturn($group);
414+
$this->repositoryMock->expects($this->once())
415+
->method('save')
416+
->willThrowException(new CouldNotSaveException(__('Lock wait timeout')));
417+
418+
$this->service->add('product_sku', 1, 100, 2);
419+
}
420+
396421
/**
397422
* @param string|int $price
398423
* @param string|float $qty

app/code/Magento/Catalog/Test/Unit/Model/ProductRepositoryTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\Catalog\Model\Layer\Filter\Dynamic\AlgorithmFactory;
1313
use Magento\Framework\Api\Data\ImageContentInterface;
1414
use Magento\Framework\Api\SortOrder;
15+
use Magento\Framework\DB\Adapter\ConnectionException;
1516
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1617
use Magento\Store\Model\ScopeInterface;
1718

@@ -602,6 +603,36 @@ public function testSaveInvalidProductException()
602603
$this->model->save($this->productMock);
603604
}
604605

606+
/**
607+
* @expectedException \Magento\Framework\Exception\TemporaryState\CouldNotSaveException
608+
* @expectedExceptionMessage Database connection error
609+
*/
610+
public function testSaveThrowsTemporaryStateExceptionIfDatabaseConnectionErrorOccurred()
611+
{
612+
$this->productFactoryMock->expects($this->any())
613+
->method('create')
614+
->will($this->returnValue($this->productMock));
615+
$this->initializationHelperMock->expects($this->never())
616+
->method('initialize');
617+
$this->resourceModelMock->expects($this->once())
618+
->method('validate')
619+
->with($this->productMock)
620+
->willReturn(true);
621+
$this->resourceModelMock->expects($this->once())
622+
->method('save')
623+
->with($this->productMock)
624+
->willThrowException(new ConnectionException('Connection lost'));
625+
$this->extensibleDataObjectConverterMock
626+
->expects($this->once())
627+
->method('toNestedArray')
628+
->will($this->returnValue($this->productData));
629+
$this->productMock->expects($this->once())
630+
->method('getWebsiteIds')
631+
->willReturn([]);
632+
633+
$this->model->save($this->productMock);
634+
}
635+
605636
public function testDelete()
606637
{
607638
$this->productMock->expects($this->exactly(2))->method('getSku')->willReturn('product-42');

lib/internal/Magento/Framework/Exception/TemporaryState/CouldNotSaveException.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@
55
*/
66
namespace Magento\Framework\Exception\TemporaryState;
77

8-
use Magento\Framework\Exception\TemporaryStateException;
8+
use Magento\Framework\Exception\TemporaryStateExceptionInterface;
9+
use Magento\Framework\Exception\CouldNotSaveException as LocalizedCouldNotSaveException;
910
use Magento\Framework\Phrase;
1011

1112
/**
1213
* CouldNotSaveException caused by recoverable error
1314
*/
14-
class CouldNotSaveException
15-
extends \Magento\Framework\Exception\CouldNotSaveException
16-
implements TemporaryStateException
15+
class CouldNotSaveException extends LocalizedCouldNotSaveException implements TemporaryStateExceptionInterface
1716
{
1817
/**
1918
* Class constructor

lib/internal/Magento/Framework/Exception/TemporaryStateException.php renamed to lib/internal/Magento/Framework/Exception/TemporaryStateExceptionInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
/**
99
* Temporary state exception that represent recoverable error
1010
*/
11-
interface TemporaryStateException
11+
interface TemporaryStateExceptionInterface
1212
{
1313
}

0 commit comments

Comments
 (0)