Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ class BulkManagement implements BulkManagementInterface
*/
private $logger;

/**
* @var Exception|null
*/
private $lastException = null;

/**
* BulkManagement constructor.
* @param EntityManager $entityManager
Expand Down Expand Up @@ -134,12 +139,23 @@ public function scheduleBulk($bulkUuid, array $operations, $description, $userId
} catch (Exception $exception) {
$connection->rollBack();
$this->logger->critical($exception->getMessage());
$this->lastException = $exception;
return false;
}

return true;
}

/**
* Return the last exception caught by scheduleBulk(), or null if none.
*
* @return Exception|null
*/
public function getLastException(): ?Exception
{
return $this->lastException;
}

/**
* Retry bulk operations that failed due to given errors.
*
Expand Down
12 changes: 10 additions & 2 deletions app/code/Magento/AsynchronousOperations/Model/MassSchedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,12 @@ public function publishMass($topicName, array $entitiesArray, $groupId = null, $

/** create new bulk without operations */
if (!$this->bulkManagement->scheduleBulk($groupId, [], $bulkDescription, $userId)) {
$previous = method_exists($this->bulkManagement, 'getLastException')
? $this->bulkManagement->getLastException()
: null;
throw new LocalizedException(
__('Something went wrong while processing the request.')
__('Something went wrong while processing the request.'),
$previous
);
}
}
Expand Down Expand Up @@ -167,11 +171,15 @@ public function publishMass($topicName, array $entitiesArray, $groupId = null, $
}

if (!$this->bulkManagement->scheduleBulk($groupId, $operations, $bulkDescription, $userId)) {
$previous = method_exists($this->bulkManagement, 'getLastException')
? $this->bulkManagement->getLastException()
: null;
try {
$this->bulkManagement->deleteBulk($groupId);
} finally {
throw new LocalizedException(
__('Something went wrong while processing the request.')
__('Something went wrong while processing the request.'),
$previous
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,35 @@ public function testScheduleBulkWithException(): void
$this->assertFalse($this->bulkManagement->scheduleBulk($bulkUuid, [$operation], $description, $userId));
}

public function testGetLastExceptionReturnsExceptionCaughtByScheduleBulk(): void
{
$bulkUuid = 'bulk-001';
$description = 'Bulk summary description...';
$userId = 1;
$connectionName = 'default';
$rootCause = new \LogicException('Root cause: queue broker unavailable');
$operation = $this->createMock(OperationInterface::class);
$metadata = $this->createMock(EntityMetadataInterface::class);

$this->metadataPool->method('getMetadata')->willReturn($metadata);
$metadata->method('getEntityConnectionName')->willReturn($connectionName);
$connection = $this->createMock(AdapterInterface::class);
$this->resourceConnection->method('getConnectionByName')->willReturn($connection);
$connection->method('beginTransaction')->willReturnSelf();
$connection->method('rollBack')->willReturnSelf();

$bulkSummary = $this->createMock(BulkSummaryInterface::class);
$this->bulkSummaryFactory->method('create')->willReturn($bulkSummary);
$this->entityManager->method('load')->willThrowException($rootCause);
$this->logger->method('critical');

$this->assertNull($this->bulkManagement->getLastException());

$this->bulkManagement->scheduleBulk($bulkUuid, [$operation], $description, $userId);

$this->assertSame($rootCause, $this->bulkManagement->getLastException());
}

/**
* Test for scheduleBulk method with exception during publishing.
*
Expand Down
Loading