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

magento-engcom/import-export-improvements#75: fix stoping on errors #130

Merged
merged 5 commits into from
Feb 15, 2019
Merged
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 @@ -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
9 changes: 2 additions & 7 deletions app/code/Magento/ImportExport/Model/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,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 @@ -620,12 +620,7 @@ public function validateSource(\Magento\ImportExport\Model\Import\AbstractSource
$messages = $this->getOperationResultMessages($errorAggregator);
$this->addLogComment($messages);

$result = !$errorAggregator->getErrorsCount();
$validationStrategy = $this->getData(self::FIELD_NAME_VALIDATION_STRATEGY);
if ($validationStrategy === ProcessingErrorAggregatorInterface::VALIDATION_STRATEGY_SKIP_ERRORS) {
$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