diff --git a/src/hashstore/filehashstore.py b/src/hashstore/filehashstore.py index 2f825ed3..3e201e77 100644 --- a/src/hashstore/filehashstore.py +++ b/src/hashstore/filehashstore.py @@ -18,6 +18,7 @@ CidRefsContentError, CidRefsDoesNotExist, CidRefsFileNotFound, + HashStoreRefsAlreadyExists, NonMatchingChecksum, NonMatchingObjSize, PidAlreadyExistsError, @@ -639,7 +640,12 @@ def tag_object(self, pid, cid): cid_refs_path, "Refs file already exists, verifying.", ) - return True + error_msg = ( + f"FileHashStore - tag_object: Object with cid: {cid}" + + f" already exists and is tagged with pid: {pid}" + ) + logging.error(error_msg) + raise HashStoreRefsAlreadyExists(error_msg) elif os.path.exists(pid_refs_path) and not os.path.exists(cid_refs_path): debug_msg = ( f"FileHashStore - tag_object: pid refs file exists ({pid_refs_path})" diff --git a/src/hashstore/filehashstore_exceptions.py b/src/hashstore/filehashstore_exceptions.py index c81f9c55..7556c3f4 100644 --- a/src/hashstore/filehashstore_exceptions.py +++ b/src/hashstore/filehashstore_exceptions.py @@ -99,6 +99,14 @@ def __init__(self, message, errors=None): self.errors = errors +class HashStoreRefsAlreadyExists(Exception): + """Custom exception thrown when called to tag an object that is already tagged appropriately.""" + + def __init__(self, message, errors=None): + super().__init__(message) + self.errors = errors + + class UnsupportedAlgorithm(Exception): """Custom exception thrown when a given algorithm is not supported in HashStore for calculating hashes/checksums, as the default store algo and/or other operations.""" diff --git a/tests/test_filehashstore_interface.py b/tests/test_filehashstore_interface.py index 578511ce..08ecb053 100644 --- a/tests/test_filehashstore_interface.py +++ b/tests/test_filehashstore_interface.py @@ -498,7 +498,11 @@ def test_store_object_duplicates_threads(pids, store): entity = "objects" def store_object_wrapper(obj_pid, obj_path): - store.store_object(obj_pid, obj_path) # Call store_object inside the thread + try: + store.store_object(obj_pid, obj_path) # Call store_object inside the thread + # pylint: disable=W0718 + except Exception as e: + assert type(e).__name__ == "HashStoreRefsAlreadyExists" thread1 = Thread(target=store_object_wrapper, args=(pid, path)) thread2 = Thread(target=store_object_wrapper, args=(pid, path)) diff --git a/tests/test_filehashstore_references.py b/tests/test_filehashstore_references.py index a1664bc5..e6d7c0f7 100644 --- a/tests/test_filehashstore_references.py +++ b/tests/test_filehashstore_references.py @@ -7,6 +7,7 @@ from hashstore.filehashstore_exceptions import ( CidRefsContentError, CidRefsFileNotFound, + HashStoreRefsAlreadyExists, NonMatchingChecksum, NonMatchingObjSize, PidAlreadyExistsError, @@ -86,7 +87,9 @@ def test_tag_object_pid_refs_found_cid_refs_found(pids, store): object_metadata = store.store_object(None, path) cid = object_metadata.cid store.tag_object(pid, cid) - store.tag_object(pid, cid) + + with pytest.raises(HashStoreRefsAlreadyExists): + store.tag_object(pid, cid) cid_refs_file_path = store._resolve_path("cid", object_metadata.cid) line_count = 0