Skip to content

Commit

Permalink
ENGCOM-3278: magento-engcom/import-export-improvements#75: fix stopin…
Browse files Browse the repository at this point in the history
…g on errors #130
  • Loading branch information
sidolov authored Feb 13, 2019
2 parents 9fbc4da + 34a2d24 commit e131ff7
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private function processValidationResult($validationResult, $resultBlock)
$resultBlock->addError(
__('Data validation failed. Please fix the following errors and upload the file again.')
);
$this->addErrorMessages($resultBlock, $errorAggregator);

if ($errorAggregator->getErrorsCount()) {
$this->addMessageToSkipErrors($resultBlock);
}
Expand All @@ -100,6 +100,8 @@ private function processValidationResult($validationResult, $resultBlock)
$errorAggregator->getErrorsCount()
)
);

$this->addErrorMessages($resultBlock, $errorAggregator);
} else {
if ($errorAggregator->getErrorsCount()) {
$this->collectErrors($resultBlock);
Expand Down
13 changes: 2 additions & 11 deletions app/code/Magento/ImportExport/Model/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ public function getOperationResultMessages(ProcessingErrorAggregatorInterface $v
{
$messages = [];
if ($this->getProcessedRowsCount()) {
if ($validationResult->getErrorsCount()) {
if ($validationResult->isErrorLimitExceeded()) {
$messages[] = __('Data validation failed. Please fix the following errors and upload the file again.');

// errors info
Expand Down Expand Up @@ -630,16 +630,7 @@ public function validateSource(\Magento\ImportExport\Model\Import\AbstractSource
$messages = $this->getOperationResultMessages($errorAggregator);
$this->addLogComment($messages);

$errorsCount = $errorAggregator->getErrorsCount();
$result = !$errorsCount;
$validationStrategy = $this->getData(self::FIELD_NAME_VALIDATION_STRATEGY);
if ($errorsCount
&& $validationStrategy === ProcessingErrorAggregatorInterface::VALIDATION_STRATEGY_SKIP_ERRORS
) {
$this->messageManager->addWarningMessage(__('Skipped errors: %1', $errorsCount));
$result = true;
}

$result = !$errorAggregator->isErrorLimitExceeded();
if ($result) {
$this->addLogComment(__('Import data validation is complete.'));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public function __construct(
}

/**
* Add error via code and level
*
* @param string $errorCode
* @param string $errorLevel
* @param int|null $rowNumber
Expand Down Expand Up @@ -96,6 +98,8 @@ public function addError(
}

/**
* Add row to be skipped during import
*
* @param int $rowNumber
* @return $this
*/
Expand All @@ -110,6 +114,8 @@ public function addRowToSkip($rowNumber)
}

/**
* Add specific row to invalid list via row number
*
* @param int $rowNumber
* @return $this
*/
Expand All @@ -126,6 +132,8 @@ protected function processInvalidRow($rowNumber)
}

/**
* Add error message template
*
* @param string $code
* @param string $template
* @return $this
Expand All @@ -138,6 +146,8 @@ public function addErrorMessageTemplate($code, $template)
}

/**
* Check if row is invalid by row number
*
* @param int $rowNumber
* @return bool
*/
Expand All @@ -147,6 +157,8 @@ public function isRowInvalid($rowNumber)
}

/**
* Get number of invalid rows
*
* @return int
*/
public function getInvalidRowsCount()
Expand All @@ -155,6 +167,8 @@ public function getInvalidRowsCount()
}

/**
* Initialize validation strategy
*
* @param string $validationStrategy
* @param int $allowedErrorCount
* @return $this
Expand All @@ -178,6 +192,8 @@ public function initValidationStrategy($validationStrategy, $allowedErrorCount =
}

/**
* Check if import has to be terminated
*
* @return bool
*/
public function hasToBeTerminated()
Expand All @@ -186,15 +202,17 @@ public function hasToBeTerminated()
}

/**
* Check if error limit has been exceeded
*
* @return bool
*/
public function isErrorLimitExceeded()
{
$isExceeded = false;
$errorsCount = $this->getErrorsCount([ProcessingError::ERROR_LEVEL_NOT_CRITICAL]);
$errorsCount = $this->getErrorsCount();
if ($errorsCount > 0
&& $this->validationStrategy == self::VALIDATION_STRATEGY_STOP_ON_ERROR
&& $errorsCount >= $this->allowedErrorsCount
&& $errorsCount > $this->allowedErrorsCount
) {
$isExceeded = true;
}
Expand All @@ -203,6 +221,8 @@ public function isErrorLimitExceeded()
}

/**
* Check if import has a fatal error
*
* @return bool
*/
public function hasFatalExceptions()
Expand All @@ -211,6 +231,8 @@ public function hasFatalExceptions()
}

/**
* Get all errors from an import process
*
* @return ProcessingError[]
*/
public function getAllErrors()
Expand All @@ -228,6 +250,8 @@ public function getAllErrors()
}

/**
* Get a specific set of errors via codes
*
* @param string[] $codes
* @return ProcessingError[]
*/
Expand All @@ -244,6 +268,8 @@ public function getErrorsByCode(array $codes)
}

/**
* Get an error via row number
*
* @param int $rowNumber
* @return ProcessingError[]
*/
Expand All @@ -258,6 +284,8 @@ public function getErrorByRowNumber($rowNumber)
}

/**
* Get a set rows via a set of error codes
*
* @param array $errorCode
* @param array $excludedCodes
* @param bool $replaceCodeWithMessage
Expand Down Expand Up @@ -292,6 +320,8 @@ public function getRowsGroupedByErrorCode(
}

/**
* Get the max allowed error count
*
* @return int
*/
public function getAllowedErrorsCount()
Expand All @@ -300,6 +330,8 @@ public function getAllowedErrorsCount()
}

/**
* Get current error count
*
* @param string[] $errorLevels
* @return int
*/
Expand All @@ -318,6 +350,8 @@ public function getErrorsCount(
}

/**
* Clear the error aggregator
*
* @return $this
*/
public function clear()
Expand All @@ -331,6 +365,8 @@ public function clear()
}

/**
* Check if an error has already been added to the aggregator
*
* @param int $rowNum
* @param string $errorCode
* @param string $columnName
Expand All @@ -348,6 +384,8 @@ protected function isErrorAlreadyAdded($rowNum, $errorCode, $columnName = null)
}

/**
* Build an error message via code, message and column name
*
* @param string $errorCode
* @param string $errorMessage
* @param string $columnName
Expand All @@ -369,6 +407,8 @@ protected function getErrorMessage($errorCode, $errorMessage, $columnName)
}

/**
* Process the error statistics for a given error level
*
* @param string $errorLevel
* @return $this
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ public function testIsErrorLimitExceededTrue()
*/
public function testIsErrorLimitExceededFalse()
{
$this->model->initValidationStrategy('validation-stop-on-errors', 5);
$this->model->addError('systemException');
$this->model->addError('systemException', 'critical', 7, 'Some column name', 'Message', 'Description');
$this->model->addError('systemException', 'critical', 4, 'Some column name', 'Message', 'Description');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<item name="entity" xsi:type="string">Advanced Pricing</item>
<item name="behavior" xsi:type="string">Add/Update</item>
<item name="validation_strategy" xsi:type="string">Stop on Error</item>
<item name="allowed_error_count" xsi:type="string">10</item>
<item name="allowed_error_count" xsi:type="string">1</item>
<item name="import_field_separator" xsi:type="string">,</item>
<item name="import_multiple_value_separator" xsi:type="string">,</item>
<item name="import_file" xsi:type="array">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,13 @@
'tier_price' => 'text',
'tier_price_value_type' => 'Fixed',
],
'data_1' => [
'sku' => '%sku%',
'tier_price_website' => "All Websites [USD]",
'tier_price_customer_group' => 'ALL GROUPS',
'tier_price_qty' => '3',
'tier_price' => 'text',
'tier_price_value_type' => 'Fixed',
],
],
];

0 comments on commit e131ff7

Please sign in to comment.