Skip to content

Commit

Permalink
Add TagError.object_id.
Browse files Browse the repository at this point in the history
For #266.
  • Loading branch information
lemon24 committed Jan 16, 2022
1 parent d89dfcd commit 00f61cb
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/reader/_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from datetime import timedelta
from functools import partial
from typing import Any
from typing import cast
from typing import Dict
from typing import Iterable
from typing import Mapping
Expand Down Expand Up @@ -1205,7 +1206,7 @@ def delete_tag(self, object_id: Tuple[str, ...], key: str) -> None:

with self.db:
cursor = self.db.execute(query, params)
rowcount_exactly_one(cursor, lambda: TagNotFoundError(key=key))
rowcount_exactly_one(cursor, lambda: TagNotFoundError(key, cast(str, None)))


def make_get_feeds_query(
Expand Down
5 changes: 3 additions & 2 deletions src/reader/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2016,7 +2016,7 @@ def get_tag(
"""
return zero_or_one(
(v for _, v in self.get_tags(resource, key=key)),
lambda: TagNotFoundError(key),
lambda: TagNotFoundError(key, _feed_argument(resource)),
default,
)

Expand Down Expand Up @@ -2078,8 +2078,9 @@ def delete_tag(
feed_url = _feed_argument(resource)
try:
self._storage.delete_tag((feed_url,), key)
except TagNotFoundError:
except TagNotFoundError as e:
if not missing_ok:
e.object_id = feed_url
raise

def make_reader_reserved_name(self, key: str) -> str:
Expand Down
14 changes: 10 additions & 4 deletions src/reader/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Any
from typing import Tuple
from typing import Union

from ._vendor.functools import cached_property

Expand Down Expand Up @@ -267,17 +268,22 @@ class TagError(ReaderError):
"""

# TODO: object_id: tuple[str, ...] | None

def __init__(self, key: str, message: str = '') -> None:
def __init__(
self, key: str, object_id: Union[str, Tuple[str, str]], message: str = ''
) -> None:
super().__init__(message)

#: The tag key.
self.key = key

# TODO: tuple[str, ...], once FeedError.object_id becomes tuple[str]

#: The resource id.
self.object_id = object_id

@property
def _str(self) -> str:
return repr(self.key)
return f"{self.object_id!r}: {self.key!r}"


class TagNotFoundError(TagError):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,5 @@ def test_metadata_error_str(exc_type):

@pytest.mark.parametrize('exc_type', all_classes(TagError))
def test_tag_error_str(exc_type):
exc = exc_type('key')
assert repr('key') in str(exc)
exc = exc_type('key', 'object')
assert "'object': 'key'" in str(exc)

0 comments on commit 00f61cb

Please sign in to comment.