diff --git a/examples/handle_errors.py b/examples/handle_errors.py new file mode 100644 index 0000000..67c373e --- /dev/null +++ b/examples/handle_errors.py @@ -0,0 +1,8 @@ +import scrython + +# oops, we asked for an exact match to a card, but failed to put the name in quotes +# that's going to throw a Scryfall error +try: + search = scrython.cards.Search(q="!Black Lotus") +except scrython.ScryfallError as e: + print(str(e.error_details['status']) + ' ' + e.error_details['code'] + ': ' + e.error_details['details']) diff --git a/scrython/__init__.py b/scrython/__init__.py index f7b74f1..4796016 100644 --- a/scrython/__init__.py +++ b/scrython/__init__.py @@ -41,6 +41,9 @@ from scrython.symbology import Symbology #Import bulk-data from scrython.bulk_data import BulkData +#Utility +from scrython.foundation import ScryfallError + __all__ = [ 'Autocomplete', 'Collector', @@ -72,5 +75,6 @@ __all__ = [ 'ArtistNames', 'ParseMana', 'Symbology', - 'BulkData' -] \ No newline at end of file + 'BulkData', + 'ScryfallError', +] diff --git a/scrython/cards/cards_object.py b/scrython/cards/cards_object.py index 38dac2b..3174569 100644 --- a/scrython/cards/cards_object.py +++ b/scrython/cards/cards_object.py @@ -174,7 +174,8 @@ class CardsObject(FoundationObject): 'double_faced_token': lambda num: self.scryfallJson['card_faces'][num]['image_uris'], 'emblem': lambda num: self.scryfallJson['image_uris'], 'augment': lambda num: self.scryfallJson['image_uris'], - 'host': lambda num: self.scryfallJson['image_uris'] + 'host': lambda num: self.scryfallJson['image_uris'], + 'adventure': lambda num: self.scryfallJson['image_uris'] } image_types = { diff --git a/scrython/foundation.py b/scrython/foundation.py index 9b984a6..844224c 100644 --- a/scrython/foundation.py +++ b/scrython/foundation.py @@ -2,6 +2,13 @@ import aiohttp import asyncio import urllib +class ScryfallError(Exception): + def __init__(self, error_obj, *args, **kwargs): + super(self.__class__, self).__init__(*args, **kwargs) + self.error_details = {} + self.error_details.update(error_obj) + + class FoundationObject(object): def __init__(self, _url, override=False, **kwargs): @@ -28,7 +35,7 @@ class FoundationObject(object): loop.run_until_complete(main(loop)) if self.scryfallJson['object'] == 'error': - raise Exception(self.scryfallJson['details']) + raise ScryfallError(self.scryfallJson, self.scryfallJson['details']) def _checkForKey(self, key): """Checks for a key in the scryfallJson object. @@ -58,4 +65,4 @@ class FoundationObject(object): KeyError: If key is not found. """ if not key in self.scryfallJson[parent][num]: - raise KeyError('This tuple has no key \'{}\''.format(key)) \ No newline at end of file + raise KeyError('This tuple has no key \'{}\''.format(key))