Skip to content

Commit

Permalink
added some error details
Browse files Browse the repository at this point in the history
Signed-off-by: Florian Steffens <florian.steffens@nextcloud.com>
  • Loading branch information
Florian Steffens committed Jan 12, 2024
1 parent 1f8edcf commit b87f19c
Showing 1 changed file with 31 additions and 31 deletions.
62 changes: 31 additions & 31 deletions lib/Service/RowService.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function findAllByTable(int $tableId, string $userId, ?int $limit = null,
}
} catch (Exception $e) {
$this->logger->error($e->getMessage());
throw new InternalError($e->getMessage());
throw new InternalError($e->getMessage(), $e->getCode(), $e);
}
}

Expand All @@ -98,7 +98,7 @@ public function findAllByView(int $viewId, string $userId, ?int $limit = null, ?
}
} catch (Exception $e) {
$this->logger->error($e->getMessage());
throw new InternalError($e->getMessage());
throw new InternalError($e->getMessage(), $e->getCode(), $e);
}
}

Expand All @@ -115,7 +115,7 @@ public function find(int $id): Row2 {
$columns = $this->columnMapper->findAllByTable($id);
} catch (Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage());
throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage(), $e->getCode(), $e);
}

try {
Expand All @@ -125,7 +125,7 @@ public function find(int $id): Row2 {
throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage(), $e->getCode(), $e);
} catch (NotFoundError $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new NotFoundError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage());
throw new NotFoundError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage(), $e->getCode(), $e);
}

// security
Expand All @@ -151,7 +151,7 @@ public function create(?int $tableId, ?int $viewId, array $data): Row2 {
if ($this->userId === null || $this->userId === '') {
$e = new \Exception('No user id in context, but needed.');
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage());
throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage(), $e->getCode(), $e);
}

if ($viewId) {
Expand All @@ -162,7 +162,7 @@ public function create(?int $tableId, ?int $viewId, array $data): Row2 {
throw new NotFoundError("Given view could not be found. More details can be found in the log.");
} catch (InternalError|Exception|MultipleObjectsReturnedException $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage());
throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage(), $e->getCode(), $e);
}

// security
Expand All @@ -176,20 +176,20 @@ public function create(?int $tableId, ?int $viewId, array $data): Row2 {
$table = $this->tableMapper->find($tableId);
} catch (DoesNotExistException $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new NotFoundError("Given table could not be found. More details can be found in the log.");
throw new NotFoundError("Given table could not be found. More details can be found in the log.", $e->getCode(), $e);
} catch (MultipleObjectsReturnedException|Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage());
throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage(), $e->getCode(), $e);
}

// security
if (!$this->permissionsService->canCreateRows($table, 'table')) {
throw new PermissionError('create row at the table id = '.$tableId.' is not allowed.');
throw new PermissionError('create row at the table id = '.$tableId.' is not allowed.', $e->getCode(), $e);

Check failure on line 187 in lib/Service/RowService.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

UndefinedVariable

lib/Service/RowService.php:187:92: UndefinedVariable: Cannot find referenced variable $e (see https://psalm.dev/024)

Check failure on line 187 in lib/Service/RowService.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

UndefinedVariable

lib/Service/RowService.php:187:107: UndefinedVariable: Cannot find referenced variable $e (see https://psalm.dev/024)

Check failure on line 187 in lib/Service/RowService.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable28

UndefinedVariable

lib/Service/RowService.php:187:92: UndefinedVariable: Cannot find referenced variable $e (see https://psalm.dev/024)

Check failure on line 187 in lib/Service/RowService.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable28

UndefinedVariable

lib/Service/RowService.php:187:107: UndefinedVariable: Cannot find referenced variable $e (see https://psalm.dev/024)
}

$columns = $this->columnMapper->findAllByTable($tableId);
} else {
throw new InternalError('Cannot create row without table or view in context');
throw new InternalError('Cannot create row without table or view in context', $e->getCode(), $e);

Check failure on line 192 in lib/Service/RowService.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

UndefinedVariable

lib/Service/RowService.php:192:82: UndefinedVariable: Cannot find referenced variable $e (see https://psalm.dev/024)

Check failure on line 192 in lib/Service/RowService.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

UndefinedVariable

lib/Service/RowService.php:192:97: UndefinedVariable: Cannot find referenced variable $e (see https://psalm.dev/024)

Check failure on line 192 in lib/Service/RowService.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable28

UndefinedVariable

lib/Service/RowService.php:192:82: UndefinedVariable: Cannot find referenced variable $e (see https://psalm.dev/024)

Check failure on line 192 in lib/Service/RowService.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable28

UndefinedVariable

lib/Service/RowService.php:192:97: UndefinedVariable: Cannot find referenced variable $e (see https://psalm.dev/024)
}

$data = $this->cleanupData($data, $columns, $tableId, $viewId);
Expand All @@ -202,7 +202,7 @@ public function create(?int $tableId, ?int $viewId, array $data): Row2 {
return $this->row2Mapper->insert($row2, $this->columnMapper->findAllByTable($tableId));
} catch (InternalError|Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage());
throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage(), $e->getCode(), $e);
}
}

Expand Down Expand Up @@ -230,7 +230,7 @@ private function cleanupData(array $data, array $columns, ?int $tableId, ?int $v
if (!$column) {
$e = new \Exception('No column found, can not parse value.');
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage());
throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage(), $e->getCode(), $e);
}

// parse given value to respect the column type value format
Expand Down Expand Up @@ -290,16 +290,16 @@ private function getRowById(int $rowId): Row2 {
if ($this->row2Mapper->getTableIdForRow($rowId) === null) {
$e = new \Exception('No table id in row, but needed.');
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage());
throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage(), $e->getCode(), $e);
}
$row = $this->row2Mapper->find($rowId, $this->columnMapper->findAllByTable($this->row2Mapper->getTableIdForRow($rowId)));
$row->markAsLoaded();
} catch (InternalError|DoesNotExistException|MultipleObjectsReturnedException|Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage());
throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage(), $e->getCode(), $e);
} catch (NotFoundError $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new NotFoundError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage());
throw new NotFoundError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage(), $e->getCode(), $e);
}
$this->tmpRows[$rowId] = $row;
return $row;
Expand Down Expand Up @@ -328,43 +328,43 @@ public function updateSet(
$item = $this->getRowById($id);
} catch (InternalError $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage());
throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage(), $e->getCode(), $e);
} catch (NotFoundError $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new NotFoundError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage());
throw new NotFoundError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage(), $e->getCode(), $e);
}

if ($viewId) {
// security
if (!$this->permissionsService->canUpdateRowsByViewId($viewId)) {
$e = new \Exception('Update row is not allowed.');
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage());
throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage(), $e->getCode(), $e);
}

try {
$view = $this->viewMapper->find($viewId);
} catch (InternalError|MultipleObjectsReturnedException|Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage());
throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage(), $e->getCode(), $e);
} catch (DoesNotExistException $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new NotFoundError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage());
throw new NotFoundError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage(), $e->getCode(), $e);
}

// is row in view?
if($this->row2Mapper->isRowInViewPresent($id, $view, $userId)) {
$e = new \Exception('Update row is not allowed.');
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage());
throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage(), $e->getCode(), $e);
}

// fetch all needed columns
try {
$columns = $this->columnMapper->findMultiple($view->getColumnsArray());
} catch (Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage());
throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage(), $e->getCode(), $e);
}
} else {
// if no view id is set, we assume a table and take the tableId from the row
Expand All @@ -374,13 +374,13 @@ public function updateSet(
if (!$this->permissionsService->canUpdateRowsByTableId($tableId)) {
$e = new \Exception('Update row is not allowed.');
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage());
throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage(), $e->getCode(), $e);
}
try {
$columns = $this->columnMapper->findAllByTable($tableId);
} catch (Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage());
throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage(), $e->getCode(), $e);
}
}

Expand Down Expand Up @@ -415,45 +415,45 @@ public function delete(int $id, ?int $viewId, string $userId): Row2 {
$item = $this->getRowById($id);
} catch (InternalError $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage());
throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage(), $e->getCode(), $e);
} catch (NotFoundError $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new NotFoundError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage());
throw new NotFoundError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage(), $e->getCode(), $e);
}

if ($viewId) {
// security
if (!$this->permissionsService->canDeleteRowsByViewId($viewId)) {
$e = new \Exception('Update row is not allowed.');
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage());
throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage(), $e->getCode(), $e);
}
try {
$view = $this->viewMapper->find($viewId);
} catch (InternalError|DoesNotExistException|MultipleObjectsReturnedException|Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage());
throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage(), $e->getCode(), $e);
}
$rowIds = $this->mapper->getRowIdsOfView($view, $userId);
if(!in_array($id, $rowIds)) {
$e = new \Exception('Update row is not allowed.');
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage());
throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage(), $e->getCode(), $e);
}
} else {
// security
if (!$this->permissionsService->canDeleteRowsByTableId($item->getTableId())) {
$e = new \Exception('Update row is not allowed.');
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new PermissionError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage());
throw new PermissionError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage(), $e->getCode(), $e);
}
}

try {
return $this->row2Mapper->delete($item);
} catch (Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage());
throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': '.$e->getMessage(), $e->getCode(), $e);
}
}

Expand Down

0 comments on commit b87f19c

Please sign in to comment.