Skip to content

Commit a102bd1

Browse files
committed
throw error on API fetch failure
1 parent a0a288d commit a102bd1

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

PyDictionary/core.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from .utils import _get_soup_object
55
except:
66
from utils import _get_soup_object
7+
from exceptions import ApiError
78

89
python2 = False
910
if list(sys.version_info)[0] == 2:
@@ -133,10 +134,12 @@ def meaning(term, disable_errors=False):
133134
name = a.text
134135
out[name] = meanings
135136
return out
136-
except Exception as e:
137+
except IndexError as e:
137138
if disable_errors == False:
138139
print("Error: The Following Error occured: %s" % e)
139140

141+
raise ApiError("Failed to fetch meaning from API")
142+
140143
if __name__ == '__main__':
141144
d = PyDictionary('honest','happy')
142145
d.printSynonyms()

PyDictionary/exceptions.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class ApiError(Exception):
2+
def __init__(self, msg):
3+
self.msg = msg
4+
def __str__(self):
5+
return self.msg

PyDictionary/test_pydictionary.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
from .__init__ import PyDictionary #Python 3
44
except:
55
from __init__ import PyDictionary
6+
from exceptions import ApiError
67

78
dictionary=PyDictionary()
89

910
class PyDictionaryTest(unittest.TestCase):
1011
def testMeaning(self):
1112
self.assertIsInstance(dictionary.meaning('python'),dict)
1213
self.assertIsInstance(dictionary.meaning("neural network"), dict)
14+
self.assertRaises(ApiError, dictionary.meaning, "blies", disable_errors=False)
15+
1316
def testSynonym(self):
1417
self.assertIsInstance(dictionary.synonym('happy'),list)
1518
def testAntonym(self):

0 commit comments

Comments
 (0)