Merge pull request #38 from vtbassmatt/patch-1
Make Scryfall errors easier to catch and handle
This commit is contained in:
commit
cce9dc869b
|
@ -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'])
|
|
@ -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',
|
||||||
|
]
|
||||||
|
|
|
@ -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.
|
||||||
|
@ -58,4 +65,4 @@ class FoundationObject(object):
|
||||||
KeyError: If key is not found.
|
KeyError: If key is not found.
|
||||||
"""
|
"""
|
||||||
if not key in self.scryfallJson[parent][num]:
|
if not key in self.scryfallJson[parent][num]:
|
||||||
raise KeyError('This tuple has no key \'{}\''.format(key))
|
raise KeyError('This tuple has no key \'{}\''.format(key))
|
||||||
|
|
Loading…
Reference in New Issue