summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Müller <thomas.mueller@tmit.eu>2015-12-03 09:26:05 +0100
committerThomas Müller <thomas.mueller@tmit.eu>2015-12-03 09:26:05 +0100
commite7239b6553e8791278396645d75225cabe00a9d8 (patch)
treee25a4c3f1b3febc651b9fbfe95ffd3d4b3cfd9f3
parenta5c80ba8bc689a67adb8fda7eba31892c3e254f9 (diff)
parent08d07cc4db58a7f11532d73108b63a2f50390bf6 (diff)
downloadnextcloud-server-e7239b6553e8791278396645d75225cabe00a9d8.tar.gz
nextcloud-server-e7239b6553e8791278396645d75225cabe00a9d8.zip
Merge pull request #20872 from owncloud/systemtags-better-not-found-exception
Systemtags better not found exception
-rw-r--r--lib/private/systemtag/systemtagmanager.php20
-rw-r--r--lib/private/systemtag/systemtagobjectmapper.php4
-rw-r--r--lib/public/systemtag/isystemtagmanager.php2
-rw-r--r--lib/public/systemtag/tagnotfoundexception.php28
-rw-r--r--tests/lib/systemtag/systemtagmanagertest.php8
-rw-r--r--tests/lib/systemtag/systemtagobjectmappertest.php8
6 files changed, 60 insertions, 10 deletions
diff --git a/lib/private/systemtag/systemtagmanager.php b/lib/private/systemtag/systemtagmanager.php
index 3cc45fc9524..8caf10d69da 100644
--- a/lib/private/systemtag/systemtagmanager.php
+++ b/lib/private/systemtag/systemtagmanager.php
@@ -94,7 +94,9 @@ class SystemTagManager implements ISystemTagManager {
$result->closeCursor();
if (count($tags) !== count($tagIds)) {
- throw new TagNotFoundException(json_encode(array_diff($tagIds, array_keys($tags))));
+ throw new TagNotFoundException(
+ 'Tag id(s) not found', 0, null, array_diff($tagIds, array_keys($tags))
+ );
}
return $tags;
@@ -218,7 +220,7 @@ class SystemTagManager implements ISystemTagManager {
try {
if ($query->execute() === 0) {
throw new TagNotFoundException(
- 'Tag ("' . $tagName . '", '. $userVisible . ', ' . $userAssignable . ') does not exist'
+ 'Tag does not exist', 0, null, [$tagId]
);
}
} catch (UniqueConstraintViolationException $e) {
@@ -238,6 +240,13 @@ class SystemTagManager implements ISystemTagManager {
$tagIds = [$tagIds];
}
+ $tagNotFoundException = null;
+ try {
+ $this->getTagsById($tagIds);
+ } catch (TagNotFoundException $e) {
+ $tagNotFoundException = $e;
+ }
+
// delete relationships first
$query = $this->connection->getQueryBuilder();
$query->delete(SystemTagObjectMapper::RELATION_TABLE)
@@ -248,11 +257,12 @@ class SystemTagManager implements ISystemTagManager {
$query = $this->connection->getQueryBuilder();
$query->delete(self::TAG_TABLE)
->where($query->expr()->in('id', $query->createParameter('tagids')))
- ->setParameter('tagids', $tagIds, Connection::PARAM_INT_ARRAY);
+ ->setParameter('tagids', $tagIds, Connection::PARAM_INT_ARRAY)
+ ->execute();
- if ($query->execute() === 0) {
+ if ($tagNotFoundException !== null) {
throw new TagNotFoundException(
- 'Tag does not exist'
+ 'Tag id(s) not found', 0, $tagNotFoundException, $tagNotFoundException->getMissingTags()
);
}
}
diff --git a/lib/private/systemtag/systemtagobjectmapper.php b/lib/private/systemtag/systemtagobjectmapper.php
index d8ff069910d..75f2631a010 100644
--- a/lib/private/systemtag/systemtagobjectmapper.php
+++ b/lib/private/systemtag/systemtagobjectmapper.php
@@ -219,7 +219,9 @@ class SystemTagObjectMapper implements ISystemTagObjectMapper {
$tags
);
$missingTagIds = array_diff($tagIds, $foundTagIds);
- throw new TagNotFoundException('Tags ' . json_encode($missingTagIds) . ' do not exist');
+ throw new TagNotFoundException(
+ 'Tags not found', 0, null, $missingTagIds
+ );
}
}
}
diff --git a/lib/public/systemtag/isystemtagmanager.php b/lib/public/systemtag/isystemtagmanager.php
index ffdaf03879e..4e3b263e56c 100644
--- a/lib/public/systemtag/isystemtagmanager.php
+++ b/lib/public/systemtag/isystemtagmanager.php
@@ -106,7 +106,7 @@ interface ISystemTagManager {
*
* @param string|array $tagIds array of tag ids
*
- * @throws \OCP\SystemTag\TagNotFoundException if tag did not exist
+ * @throws \OCP\SystemTag\TagNotFoundException if at least one tag did not exist
*
* @since 9.0.0
*/
diff --git a/lib/public/systemtag/tagnotfoundexception.php b/lib/public/systemtag/tagnotfoundexception.php
index 94bb07c8c12..d0a35eb59e9 100644
--- a/lib/public/systemtag/tagnotfoundexception.php
+++ b/lib/public/systemtag/tagnotfoundexception.php
@@ -26,4 +26,30 @@ namespace OCP\SystemTag;
*
* @since 9.0.0
*/
-class TagNotFoundException extends \RuntimeException {}
+class TagNotFoundException extends \RuntimeException {
+
+ /** @var string[] */
+ protected $tags;
+
+ /**
+ * TagNotFoundException constructor.
+ *
+ * @param string $message
+ * @param int $code
+ * @param \Exception $previous
+ * @param string[] $tags
+ * @since 9.0.0
+ */
+ public function __construct($message = '', $code = 0, \Exception $previous = null, array $tags = []) {
+ parent::__construct($message, $code, $previous);
+ $this->tags = $tags;
+ }
+
+ /**
+ * @return string[]
+ * @since 9.0.0
+ */
+ public function getMissingTags() {
+ return $this->tags;
+ }
+}
diff --git a/tests/lib/systemtag/systemtagmanagertest.php b/tests/lib/systemtag/systemtagmanagertest.php
index 0a192f01f41..8498b85519f 100644
--- a/tests/lib/systemtag/systemtagmanagertest.php
+++ b/tests/lib/systemtag/systemtagmanagertest.php
@@ -40,9 +40,15 @@ class SystemTagManagerTest extends TestCase {
$this->connection = \OC::$server->getDatabaseConnection();
$this->tagManager = new SystemTagManager($this->connection);
- }
+ $this->pruneTagsTables();
+ }
public function tearDown() {
+ $this->pruneTagsTables();
+ parent::tearDown();
+ }
+
+ protected function pruneTagsTables() {
$query = $this->connection->getQueryBuilder();
$query->delete(SystemTagObjectMapper::RELATION_TABLE)->execute();
$query->delete(SystemTagManager::TAG_TABLE)->execute();
diff --git a/tests/lib/systemtag/systemtagobjectmappertest.php b/tests/lib/systemtag/systemtagobjectmappertest.php
index a4312fa722d..43d0b8c6960 100644
--- a/tests/lib/systemtag/systemtagobjectmappertest.php
+++ b/tests/lib/systemtag/systemtagobjectmappertest.php
@@ -62,6 +62,7 @@ class SystemTagObjectMapperTest extends TestCase {
parent::setUp();
$this->connection = \OC::$server->getDatabaseConnection();
+ $this->pruneTagsTables();
$this->tagManager = $this->getMockBuilder('OCP\SystemTag\ISystemTagManager')
->getMock();
@@ -92,9 +93,14 @@ class SystemTagObjectMapperTest extends TestCase {
$this->tagMapper->assignTags(1, 'testtype', $this->tag2->getId());
$this->tagMapper->assignTags(2, 'testtype', $this->tag1->getId());
$this->tagMapper->assignTags(3, 'anothertype', $this->tag1->getId());
- }
+ }
public function tearDown() {
+ $this->pruneTagsTables();
+ parent::tearDown();
+ }
+
+ protected function pruneTagsTables() {
$query = $this->connection->getQueryBuilder();
$query->delete(SystemTagObjectMapper::RELATION_TABLE)->execute();
$query->delete(SystemTagManager::TAG_TABLE)->execute();