Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Catch any error that may occur during row import #882

Merged
merged 1 commit into from
Feb 29, 2024
Merged
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
80 changes: 45 additions & 35 deletions lib/Service/ImportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,48 +195,55 @@ private function createRow(Row $row): void {
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false);

$i = -1;
$data = [];
foreach ($cellIterator as $cell) {
$i++;
try {
$i = -1;
$data = [];
$hasData = false;
foreach ($cellIterator as $cell) {
$i++;

// only add the dataset if column is known
if($this->columns[$i] === '' || !isset($this->columns[$i])) {
$this->logger->debug('Column unknown while fetching rows data for importing.');
continue;
}
// only add the dataset if column is known
if(!isset($this->columns[$i]) || $this->columns[$i] === '') {
$this->logger->debug('Column unknown while fetching rows data for importing.');
continue;
}

/** @var Column $column */
$column = $this->columns[$i];
/** @var Column $column */
$column = $this->columns[$i];

// if cell is empty
if(!$cell || $cell->getValue() === null) {
$this->logger->info('Cell is empty while fetching rows data for importing.');
if($column->getMandatory()) {
$this->logger->warning('Mandatory column was not set');
$this->countErrors++;
return;
// if cell is empty
if(!$cell || $cell->getValue() === null) {
$this->logger->info('Cell is empty while fetching rows data for importing.');
if($column->getMandatory()) {
$this->logger->warning('Mandatory column was not set');
$this->countErrors++;
return;
}
continue;
}

$value = $cell->getValue();
$hasData = $hasData || !empty($value);
if ($column->getType() === 'datetime') {
$value = Date::excelToDateTimeObject($value)->format('Y-m-d H:i');
} elseif ($column->getType() === 'number' && $column->getNumberSuffix() === '%') {
$value = $value * 100;
} elseif ($column->getType() === 'selection' && $column->getSubtype() === 'check') {
$value = $cell->getFormattedValue() === 'TRUE' ? 'true' : 'false';
}
continue;
}

$value = $cell->getValue();
if ($column->getType() === 'datetime') {
$value = Date::excelToDateTimeObject($value)->format('Y-m-d H:i');
} elseif ($column->getType() === 'number' && $column->getNumberSuffix() === '%') {
$value = $value * 100;
} elseif ($column->getType() === 'selection' && $column->getSubtype() === 'check') {
$value = $cell->getFormattedValue() === 'TRUE' ? 'true' : 'false';
$data[] = [
'columnId' => $column->getId(),
'value' => json_decode($this->parseValueByColumnType($value, $column)),
];
}

$data[] = [
'columnId' => (int) $this->columns[$i]->getId(),
'value' => json_decode($this->parseValueByColumnType($value, $this->columns[$i])),
];
}
try {
$this->rowService->create($this->tableId, $this->viewId, $data);
$this->countInsertedRows++;
if ($hasData) {
$this->rowService->create($this->tableId, $this->viewId, $data);
$this->countInsertedRows++;
} else {
$this->logger->debug('Skipped empty row ' . $row->getRowIndex() . ' during import');
}
} catch (PermissionError $e) {
$this->logger->error('Could not create row while importing, no permission.', ['exception' => $e]);
$this->countErrors++;
Expand All @@ -246,6 +253,9 @@ private function createRow(Row $row): void {
} catch (NotFoundError $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new NotFoundError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage());
} catch (\Throwable $e) {
$this->countErrors++;
$this->logger->error('Error while creating new row for import.', ['exception' => $e]);
}

}
Expand Down
Loading