Skip to content

Commit

Permalink
throw error on API fetch failure
Browse files Browse the repository at this point in the history
  • Loading branch information
123vivekr committed Sep 10, 2022
1 parent a0a288d commit a102bd1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
5 changes: 4 additions & 1 deletion PyDictionary/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from .utils import _get_soup_object
except:
from utils import _get_soup_object
from exceptions import ApiError

python2 = False
if list(sys.version_info)[0] == 2:
Expand Down Expand Up @@ -133,10 +134,12 @@ def meaning(term, disable_errors=False):
name = a.text
out[name] = meanings
return out
except Exception as e:
except IndexError as e:
if disable_errors == False:
print("Error: The Following Error occured: %s" % e)

raise ApiError("Failed to fetch meaning from API")

if __name__ == '__main__':
d = PyDictionary('honest','happy')
d.printSynonyms()
Expand Down
5 changes: 5 additions & 0 deletions PyDictionary/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class ApiError(Exception):
def __init__(self, msg):
self.msg = msg
def __str__(self):
return self.msg
3 changes: 3 additions & 0 deletions PyDictionary/test_pydictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
from .__init__ import PyDictionary #Python 3
except:
from __init__ import PyDictionary
from exceptions import ApiError

dictionary=PyDictionary()

class PyDictionaryTest(unittest.TestCase):
def testMeaning(self):
self.assertIsInstance(dictionary.meaning('python'),dict)
self.assertIsInstance(dictionary.meaning("neural network"), dict)
self.assertRaises(ApiError, dictionary.meaning, "blies", disable_errors=False)

def testSynonym(self):
self.assertIsInstance(dictionary.synonym('happy'),list)
def testAntonym(self):
Expand Down

0 comments on commit a102bd1

Please sign in to comment.