started improving exception handling

This commit is contained in:
Jonathan Treffler 2023-09-20 22:53:02 +02:00 committed by root
parent 1756d541ca
commit 2b64e8969a
2 changed files with 18 additions and 3 deletions

View file

@ -0,0 +1,14 @@
<?php
namespace OCA\GroupfolderFilesystemSnapshots\Exceptions;
class NotFoundException extends \RuntimeException {
public function __construct($entity, array|string $criteria) {
$message = sprintf(
"Could not find %s with criteria %s",
class_exists($entity) ? array_pop(explode('\\', $entity)) : $entity,
is_string($criteria) ? $criteria : json_encode($criteria),
);
parent::__construct($message);
}
}

View file

@ -6,6 +6,7 @@ use Exception;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCA\GroupfolderFilesystemSnapshots\Exceptions\NotFoundException;
use OCA\GroupfolderFilesystemSnapshots\Db\DiffTask;
use OCA\GroupfolderFilesystemSnapshots\Db\DiffTaskMapper;
use OCA\GroupfolderFilesystemSnapshots\Db\DiffTaskResult;
@ -36,10 +37,10 @@ class DiffTaskService {
/**
* @return never
*/
private function handleException ($e) {
private function handleException ($e, $criteria) {
if ($e instanceof DoesNotExistException ||
$e instanceof MultipleObjectsReturnedException) {
throw new NotFoundException($e->getMessage());
throw new NotFoundException($e->getMessage(), $criteria);
} else {
throw $e;
}
@ -49,7 +50,7 @@ class DiffTaskService {
try {
return $this->mapper->find($id, $userId);
} catch(Exception $e) {
$this->handleException($e);
$this->handleException($e, ["userId" => $userId, "id" => $id]);
}
}