Add a method to get the list of tags from the TagNotFound Exception

This commit is contained in:
Joas Schilling 2015-12-01 14:51:26 +01:00
parent 490e01e383
commit 1d0c041ac8
5 changed files with 47 additions and 9 deletions

View File

@ -94,7 +94,9 @@ class SystemTagManager implements ISystemTagManager {
$result->closeCursor(); $result->closeCursor();
if (count($tags) !== count($tagIds)) { 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; return $tags;
@ -218,7 +220,7 @@ class SystemTagManager implements ISystemTagManager {
try { try {
if ($query->execute() === 0) { if ($query->execute() === 0) {
throw new TagNotFoundException( throw new TagNotFoundException(
'Tag ("' . $tagName . '", '. $userVisible . ', ' . $userAssignable . ') does not exist' 'Tag does not exist', 0, null, [$tagId]
); );
} }
} catch (UniqueConstraintViolationException $e) { } catch (UniqueConstraintViolationException $e) {
@ -238,6 +240,13 @@ class SystemTagManager implements ISystemTagManager {
$tagIds = [$tagIds]; $tagIds = [$tagIds];
} }
$tagNotFoundException = null;
try {
$this->getTagsById($tagIds);
} catch (TagNotFoundException $e) {
$tagNotFoundException = $e;
}
// delete relationships first // delete relationships first
$query = $this->connection->getQueryBuilder(); $query = $this->connection->getQueryBuilder();
$query->delete(SystemTagObjectMapper::RELATION_TABLE) $query->delete(SystemTagObjectMapper::RELATION_TABLE)
@ -248,11 +257,12 @@ class SystemTagManager implements ISystemTagManager {
$query = $this->connection->getQueryBuilder(); $query = $this->connection->getQueryBuilder();
$query->delete(self::TAG_TABLE) $query->delete(self::TAG_TABLE)
->where($query->expr()->in('id', $query->createParameter('tagids'))) ->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( throw new TagNotFoundException(
'Tag does not exist' 'Tag id(s) not found', 0, $tagNotFoundException, $tagNotFoundException->getMissingTags()
); );
} }
} }

View File

@ -219,7 +219,9 @@ class SystemTagObjectMapper implements ISystemTagObjectMapper {
$tags $tags
); );
$missingTagIds = array_diff($tagIds, $foundTagIds); $missingTagIds = array_diff($tagIds, $foundTagIds);
throw new TagNotFoundException('Tags ' . json_encode($missingTagIds) . ' do not exist'); throw new TagNotFoundException(
'Tags not found', 0, null, $missingTagIds
);
} }
} }
} }

View File

@ -106,7 +106,7 @@ interface ISystemTagManager {
* *
* @param string|array $tagIds array of tag ids * @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 * @since 9.0.0
*/ */

View File

@ -26,4 +26,30 @@ namespace OCP\SystemTag;
* *
* @since 9.0.0 * @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;
}
}

View File

@ -40,7 +40,7 @@ class SystemTagManagerTest extends TestCase {
$this->connection = \OC::$server->getDatabaseConnection(); $this->connection = \OC::$server->getDatabaseConnection();
$this->tagManager = new SystemTagManager($this->connection); $this->tagManager = new SystemTagManager($this->connection);
} }
public function tearDown() { public function tearDown() {
$query = $this->connection->getQueryBuilder(); $query = $this->connection->getQueryBuilder();