From 20256fa38edfeb609018cfb86eab9bdcc25af7d8 Mon Sep 17 00:00:00 2001 From: Matt Cooper Date: Sat, 31 Aug 2019 06:53:00 -0400 Subject: [PATCH 1/5] Make Scryfall errors easier to catch and handle --- scrython/foundation.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/scrython/foundation.py b/scrython/foundation.py index 9b984a6..f72dc73 100644 --- a/scrython/foundation.py +++ b/scrython/foundation.py @@ -2,6 +2,12 @@ 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 = **error_obj + + class FoundationObject(object): def __init__(self, _url, override=False, **kwargs): @@ -28,7 +34,7 @@ class FoundationObject(object): loop.run_until_complete(main(loop)) if self.scryfallJson['object'] == 'error': - raise Exception(self.scryfallJson['details']) + raise ScryfallError(self.scryfallJson, message=self.scryfallJson['details']) def _checkForKey(self, key): """Checks for a key in the scryfallJson object. @@ -58,4 +64,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)) From 78bcc281cd67e160dbda233457cb28dd83335649 Mon Sep 17 00:00:00 2001 From: Matt Cooper Date: Sat, 31 Aug 2019 07:00:55 -0400 Subject: [PATCH 2/5] add exception to __init__ --- scrython/__init__.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) 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', +] From 5b6412f849f37c2e106f2aa291776939cd6cf71a Mon Sep 17 00:00:00 2001 From: Matt Cooper Date: Sat, 31 Aug 2019 07:02:58 -0400 Subject: [PATCH 3/5] show how to handle errors --- examples/handle_errors.py | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 examples/handle_errors.py diff --git a/examples/handle_errors.py b/examples/handle_errors.py new file mode 100644 index 0000000..7120d4e --- /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.status) + ' ' + e.code + ': ' + e.details) From 406d74c258497a5b4a4c8f5ef5f9aec6b1803e04 Mon Sep 17 00:00:00 2001 From: Matt Cooper Date: Sat, 31 Aug 2019 12:02:28 -0400 Subject: [PATCH 4/5] fix illegal use of ** --- scrython/foundation.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scrython/foundation.py b/scrython/foundation.py index f72dc73..cbe5ad1 100644 --- a/scrython/foundation.py +++ b/scrython/foundation.py @@ -5,7 +5,8 @@ import urllib class ScryfallError(Exception): def __init__(self, error_obj, *args, **kwargs): super(self.__class__, self).__init__(*args, **kwargs) - self.error_details = **error_obj + self.error_details = {} + self.error_details.update(error_obj) class FoundationObject(object): From 7bbc9b1455a128ee4ce89ccf354d5893e20f0376 Mon Sep 17 00:00:00 2001 From: Matt Cooper Date: Tue, 3 Sep 2019 09:31:47 -0400 Subject: [PATCH 5/5] fix silly bugs --- examples/handle_errors.py | 2 +- scrython/foundation.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/handle_errors.py b/examples/handle_errors.py index 7120d4e..67c373e 100644 --- a/examples/handle_errors.py +++ b/examples/handle_errors.py @@ -5,4 +5,4 @@ import scrython try: search = scrython.cards.Search(q="!Black Lotus") except scrython.ScryfallError as e: - print(str(e.status) + ' ' + e.code + ': ' + e.details) + print(str(e.error_details['status']) + ' ' + e.error_details['code'] + ': ' + e.error_details['details']) diff --git a/scrython/foundation.py b/scrython/foundation.py index cbe5ad1..844224c 100644 --- a/scrython/foundation.py +++ b/scrython/foundation.py @@ -35,7 +35,7 @@ class FoundationObject(object): loop.run_until_complete(main(loop)) if self.scryfallJson['object'] == 'error': - raise ScryfallError(self.scryfallJson, message=self.scryfallJson['details']) + raise ScryfallError(self.scryfallJson, self.scryfallJson['details']) def _checkForKey(self, key): """Checks for a key in the scryfallJson object.