Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit 3fe794a

Browse files
author
Vasiliev.A
committed
fix catching exception for unresolved inputParams, save rejected operation to magento_operation table, change AsyncResponse operation list id from key index to OperationID
1 parent 64ddf04 commit 3fe794a

File tree

2 files changed

+69
-25
lines changed

2 files changed

+69
-25
lines changed

app/code/Magento/WebapiAsync/Controller/Rest/Async/InputParamsResolver.php

+13-2
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,20 @@ public function resolve()
9191
//simple check if async request have single or bulk entities
9292
if (array_key_exists(0, $inputData)) {
9393
foreach ($inputData as $key => $singleParams) {
94-
$webapiResolvedParams[$key] = $this->resolveParams($singleParams);
94+
try {
95+
$webapiResolvedParams[$key] = $this->resolveParams($singleParams);
96+
} catch (\Exception $e) {
97+
//return input request data when failed to process rejected type in MassSchedule
98+
$webapiResolvedParams[$key] = $singleParams;
99+
}
95100
}
96101
} else {//single item request
97-
$webapiResolvedParams[] = $this->resolveParams($inputData);
102+
try {
103+
$webapiResolvedParams[] = $this->resolveParams($inputData);
104+
} catch (\Exception $e) {
105+
//return input request data when failed to process rejected type in MassSchedule
106+
$webapiResolvedParams[] = $inputData;
107+
}
98108
}
99109

100110
return $webapiResolvedParams;
@@ -132,6 +142,7 @@ private function resolveParams($inputData)
132142

133143
$inputData = $this->paramsOverrider->override($inputData, $route->getParameters());
134144
$inputParams = $this->serviceInputProcessor->process($serviceClassName, $serviceMethodName, $inputData);
145+
135146
return $inputParams;
136147
}
137148
}

app/code/Magento/WebapiAsync/Model/MessageQueue/MassSchedule.php

+56-23
Original file line numberDiff line numberDiff line change
@@ -175,39 +175,33 @@ public function publishMass($topicName, $entitiesArray, $groupId = null)
175175

176176
$operations = [];
177177
$requestItems = [];
178-
foreach ($entitiesArray as $key => $entityParams) {
178+
foreach ($entitiesArray as $entityParams) {
179179
/** @var \Magento\WebapiAsync\Api\Data\AsyncResponse\ItemStatusInterface $requestItem */
180180
$requestItem = $this->itemStatusInterfaceFactory->create();
181181

182182
try {
183183
$this->messageValidator->validate($topicName, $entityParams);
184184
$data = $this->messageEncoder->encode($topicName, $entityParams);
185-
186-
$serializedData = [
187-
'entity_id' => null,
188-
'entity_link' => '',
189-
'meta_information' => $data,
190-
];
191-
$data = [
192-
'data' => [
193-
OperationInterface::BULK_ID => $groupId,
194-
OperationInterface::TOPIC_NAME => $topicName,
195-
OperationInterface::SERIALIZED_DATA => $this->jsonHelper->serialize($serializedData),
196-
OperationInterface::STATUS => OperationInterface::STATUS_TYPE_OPEN,
197-
],
198-
];
199-
200-
/** @var \Magento\AsynchronousOperations\Api\Data\OperationInterface $operation */
201-
$operation = $this->operationFactory->create($data);
202-
$operations[] = $this->entityManager->save($operation);
203-
204-
$requestItem->setId($key);
205-
$requestItem->setStatus(ItemStatusInterface::STATUS_ACCEPTED);
185+
$operationStatus = OperationInterface::STATUS_TYPE_OPEN;
206186
} catch (\Exception $exception) {
207-
$requestItem->setId($key);
187+
$data = $entityParams;
188+
//TODO after merge with BulkApi Status need change cons from OperationInterface::STATUS_TYPE_NOT_RETRIABLY_FAILED to OperationInterface::STATUS_TYPE_REJECTED
189+
$operationStatus = OperationInterface::STATUS_TYPE_NOT_RETRIABLY_FAILED;
190+
}
191+
192+
$operation = $this->saveOperation($groupId, $topicName, $data, $operationStatus);
193+
if (!isset($exception)) {
194+
$operations[] = $operation;
195+
}
196+
197+
$requestItem->setId($operation->getId());
198+
$requestItem->setStatus(ItemStatusInterface::STATUS_ACCEPTED);
199+
200+
if (isset($exception)) {
208201
$requestItem->setStatus(ItemStatusInterface::STATUS_REJECTED);
209202
$requestItem->setErrorMessage($exception);
210203
$requestItem->setErrorCode($exception);
204+
unset($exception);
211205
}
212206

213207
$requestItems[] = $requestItem;
@@ -227,4 +221,43 @@ public function publishMass($topicName, $entitiesArray, $groupId = null)
227221

228222
return $asyncResponse;
229223
}
224+
225+
/**
226+
* @param string $groupId
227+
* @param string $topicName
228+
* @param mixed $data
229+
* @param int $operationStatus
230+
* @param null $error
231+
* @param null $errorCode
232+
* @return \Magento\AsynchronousOperations\Api\Data\OperationInterface
233+
*/
234+
private function saveOperation(
235+
$groupId,
236+
$topicName,
237+
$data,
238+
$operationStatus = OperationInterface::STATUS_TYPE_OPEN,
239+
$error = null,
240+
$errorCode = null
241+
) {
242+
$serializedData = [
243+
'entity_id' => null,
244+
'entity_link' => '',
245+
'meta_information' => $data,
246+
];
247+
$data = [
248+
'data' => [
249+
OperationInterface::BULK_ID => $groupId,
250+
OperationInterface::TOPIC_NAME => $topicName,
251+
OperationInterface::SERIALIZED_DATA => $this->jsonHelper->serialize($serializedData),
252+
OperationInterface::STATUS => $operationStatus,
253+
OperationInterface::RESULT_MESSAGE => $error,
254+
OperationInterface::ERROR_CODE => $errorCode,
255+
],
256+
];
257+
258+
/** @var \Magento\AsynchronousOperations\Api\Data\OperationInterface $operation */
259+
$operation = $this->operationFactory->create($data);
260+
261+
return $this->entityManager->save($operation);
262+
}
230263
}

0 commit comments

Comments
 (0)