diff --git a/encyclopaedia/book/book_ren.py b/encyclopaedia/book/book_ren.py index d112b45..741cb72 100644 --- a/encyclopaedia/book/book_ren.py +++ b/encyclopaedia/book/book_ren.py @@ -219,10 +219,15 @@ def add_entry(self, entry: 'EncEntry') -> bool: Raises: AddEntryError """ - if entry.parent is not None and entry.parent != self: - raise AddEntryError( - f"<{entry}> is already a page of {self.parent}", - ) + if entry.parent is not None: + if entry.parent == self: + raise AddEntryError( + f"<{entry}> is already a page of <{self}>", + ) + else: + raise AddEntryError( + f"<{entry}> already has a parent: <{entry.parent}>", + ) if entry.number is None: raise AddEntryError(f"<{entry} does not have a number.") diff --git a/encyclopaedia/encyclopaedia_ren.py b/encyclopaedia/encyclopaedia_ren.py index 6108de7..affc89d 100644 --- a/encyclopaedia/encyclopaedia_ren.py +++ b/encyclopaedia/encyclopaedia_ren.py @@ -17,6 +17,7 @@ ToggleShowLockedEntryAction, ) from .entry_sorting_ren import push_locked_to_bottom +from .exceptions_ren import AddEntryError from .eventemitter_ren import EventEmitter from .constants_ren import Direction, SortMode from .book import Book @@ -253,14 +254,14 @@ def add_entry(self, entry: ENTRY_TYPE) -> None: entry: The Entry to add to the Encyclopaedia """ if entry.parent is not None and entry.parent != self: - raise ValueError( - f"{entry} is already inside another Encyclopaedia", + raise AddEntryError( + f"<{entry}> already has a parent: <{entry.parent}>", ) # When a new entry has a number, ensure it's not already used. if entry.number is not None: if any(i for i in self.all_entries if i.number == entry.number): - raise ValueError(f"{entry.number} is already taken.") + raise AddEntryError(f"{entry.number} is already taken.") elif entry.number is None: entry.number = self._find_closest_free_number() diff --git a/encyclopaedia/exceptions_ren.py b/encyclopaedia/exceptions_ren.py index e6828ce..934ce81 100644 --- a/encyclopaedia/exceptions_ren.py +++ b/encyclopaedia/exceptions_ren.py @@ -3,5 +3,5 @@ """ class AddEntryError(Exception): - """Raised when adding an EncEntry to a parent fails.""" + """Raised when adding an entry to a parent fails.""" pass diff --git a/tests/test_encyclopaedia.py b/tests/test_encyclopaedia.py index ea801dc..79f4e3c 100644 --- a/tests/test_encyclopaedia.py +++ b/tests/test_encyclopaedia.py @@ -3,7 +3,7 @@ from encyclopaedia import Encyclopaedia from encyclopaedia import EncEntry from encyclopaedia import constants_ren - +from encyclopaedia import exceptions_ren def test_setting_entry_number(add_dummy_entries): """ @@ -248,7 +248,7 @@ def test_duplicate_entry_numbers(): locked=False, ) - with pytest.raises(ValueError) as e: + with pytest.raises(exceptions_ren.AddEntryError) as e: EncEntry( parent=enc, number=1, @@ -444,7 +444,7 @@ def test_add_entry_already_in_page(): text=["Test Text"], ) - with pytest.raises(ValueError): + with pytest.raises(exceptions_ren.AddEntryError): enc.add_entry(ee)