Skip to content

Commit

Permalink
fix: Catch any error that may occur during row import
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Härtl <jus@bitgrid.net>
  • Loading branch information
juliusknorr committed Feb 29, 2024
1 parent 12455fd commit 559d2de
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 |= !empty($value);

Check failure on line 226 in lib/Service/ImportService.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

InvalidOperand

lib/Service/ImportService.php:226:17: InvalidOperand: Cannot perform a numeric operation with a non-numeric type bool (see https://psalm.dev/058)

Check failure on line 226 in lib/Service/ImportService.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable28

InvalidOperand

lib/Service/ImportService.php:226:17: InvalidOperand: Cannot perform a numeric operation with a non-numeric type bool (see https://psalm.dev/058)
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' => (int) $this->columns[$i]->getId(),
'value' => json_decode($this->parseValueByColumnType($cell, $value, $this->columns[$i])),

Check failure on line 237 in lib/Service/ImportService.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

TooManyArguments

lib/Service/ImportService.php:237:36: TooManyArguments: Too many arguments for method OCA\Tables\Service\ImportService::parsevaluebycolumntype - saw 3 (see https://psalm.dev/026)

Check failure on line 237 in lib/Service/ImportService.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

ImplicitToStringCast

lib/Service/ImportService.php:237:59: ImplicitToStringCast: Argument 1 of OCA\Tables\Service\ImportService::parseValueByColumnType expects string, but PhpOffice\PhpSpreadsheet\Cell\Cell provided with a __toString method (see https://psalm.dev/060)

Check failure on line 237 in lib/Service/ImportService.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable28

TooManyArguments

lib/Service/ImportService.php:237:36: TooManyArguments: Too many arguments for method OCA\Tables\Service\ImportService::parsevaluebycolumntype - saw 3 (see https://psalm.dev/026)

Check failure on line 237 in lib/Service/ImportService.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable28

ImplicitToStringCast

lib/Service/ImportService.php:237:59: ImplicitToStringCast: Argument 1 of OCA\Tables\Service\ImportService::parseValueByColumnType expects string, but PhpOffice\PhpSpreadsheet\Cell\Cell provided with a __toString method (see https://psalm.dev/060)
];
}

$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 559d2de

Please sign in to comment.