Skip to content

Commit

Permalink
Merge pull request #882 from nextcloud/bugfix/import-catch
Browse files Browse the repository at this point in the history
fix: Catch any error that may occur during row import
  • Loading branch information
juliusknorr authored Feb 29, 2024
2 parents 12455fd + 37f5e09 commit 0b5575c
Showing 1 changed file with 45 additions and 35 deletions.
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

0 comments on commit 0b5575c

Please sign in to comment.