Merge pull request #38 from vtbassmatt/patch-1

Make Scryfall errors easier to catch and handle
This commit is contained in:
Nanda Scott 2019-09-24 18:58:27 -04:00 committed by GitHub
commit cce9dc869b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 4 deletions

View File

@ -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'])

View File

@ -41,6 +41,9 @@ from scrython.symbology import Symbology
#Import bulk-data #Import bulk-data
from scrython.bulk_data import BulkData from scrython.bulk_data import BulkData
#Utility
from scrython.foundation import ScryfallError
__all__ = [ __all__ = [
'Autocomplete', 'Autocomplete',
'Collector', 'Collector',
@ -72,5 +75,6 @@ __all__ = [
'ArtistNames', 'ArtistNames',
'ParseMana', 'ParseMana',
'Symbology', 'Symbology',
'BulkData' 'BulkData',
'ScryfallError',
] ]

View File

@ -2,6 +2,13 @@ import aiohttp
import asyncio import asyncio
import urllib 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): class FoundationObject(object):
def __init__(self, _url, override=False, **kwargs): def __init__(self, _url, override=False, **kwargs):
@ -28,7 +35,7 @@ class FoundationObject(object):
loop.run_until_complete(main(loop)) loop.run_until_complete(main(loop))
if self.scryfallJson['object'] == 'error': if self.scryfallJson['object'] == 'error':
raise Exception(self.scryfallJson['details']) raise ScryfallError(self.scryfallJson, self.scryfallJson['details'])
def _checkForKey(self, key): def _checkForKey(self, key):
"""Checks for a key in the scryfallJson object. """Checks for a key in the scryfallJson object.