Skip to content

Commit

Permalink
by-AttributeError/KeyError-catchable ObjectMapper attribute misses (#27)
Browse files Browse the repository at this point in the history
To prevent any unintended incompatibility with codes may be using
KeyError to catch that cases, created MissingFieldError inherited
both AttributeError and KeyError.
  • Loading branch information
yoloseem authored and youknowone committed Jun 29, 2016
1 parent 45a9cae commit 0e615e4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
4 changes: 4 additions & 0 deletions itunesiap/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@ class InvalidReceipt(RequestError):
@property
def description(self):
return self._descriptions.get(self.status, None)


class MissingFieldError(E, AttributeError, KeyError):
pass
24 changes: 14 additions & 10 deletions itunesiap/receipt.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import warnings
import json

from .exceptions import MissingFieldError
from .tools import lazy_property


Expand Down Expand Up @@ -57,16 +58,19 @@ def __getattr__(self, item):
try:
return super(ObjectMapper, self).__getattr__(item)
except AttributeError:
if item.startswith('_'):
key = item[1:]
if key not in self.__WHITELIST__:
warnings.warn('Given key `{0}` is not in __WHITELIST__. It maybe a wrong key. Check raw data `_` for real receipt data.'.format(key))
return self._[key]
if item in self.__EXPORT_FILTERS__:
filter = self.__EXPORT_FILTERS__[item]
return filter(self._[item])
if item in self.__WHITELIST__:
return self._[item]
try:
if item.startswith('_'):
key = item[1:]
if key not in self.__WHITELIST__:
warnings.warn('Given key `{0}` is not in __WHITELIST__. It maybe a wrong key. Check raw data `_` for real receipt data.'.format(key))
return self._[key]
if item in self.__EXPORT_FILTERS__:
filter = self.__EXPORT_FILTERS__[item]
return filter(self._[item])
if item in self.__WHITELIST__:
return self._[item]
except KeyError:
raise MissingFieldError(item)
return super(ObjectMapper, self).__getattribute__(item)


Expand Down
6 changes: 6 additions & 0 deletions tests/itunesiap_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,12 @@ def test_receipt():
# and that the last_in_app alias is set up correctly
assert response.receipt.last_in_app == in_app[-1]

with pytest.raises(AttributeError):
response.receipt.in_app[0].expires_date
with pytest.raises(KeyError):
response.receipt.in_app[0].expires_date
# ensure we can also catch this with KeyError due to backward compatibility


def test_shortcut():
"""Test shortcuts"""
Expand Down

0 comments on commit 0e615e4

Please sign in to comment.