Skip to content

Commit 1a0e782

Browse files
authored
Feat/wip exceptions (#65)
* feat: add some exceptions * feat: get rid of system exits from the code The code should throw exceptions instead of halting the execution of programs. Tests are also updated to account for this change. - Github Issue: #64 Authored-by: Vinit Kumar <[email protected]> Signed-off-by: Vinit Kumar <[email protected]> * feat: bump version
1 parent b2f54aa commit 1a0e782

File tree

3 files changed

+27
-19
lines changed

3 files changed

+27
-19
lines changed

json2xml/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
__author__ = """Vinit Kumar"""
66
__email__ = "[email protected]"
7-
__version__ = "3.5.0"
7+
__version__ = "3.6.0"
88

99

1010
# from .utils import readfromurl, readfromstring, readfromjson

json2xml/utils.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
"""Utils methods to convert XML data to dict from various sources"""
2-
import sys
32
import json
43
import requests
54

65

6+
class JSONReadError(Exception):
7+
pass
8+
9+
10+
class URLReadError(Exception):
11+
pass
12+
13+
14+
class StringReadError(Exception):
15+
pass
16+
17+
18+
719
def readfromjson(filename: str) -> dict:
820
"""
921
Reads a json string and emits json string
@@ -15,10 +27,10 @@ def readfromjson(filename: str) -> dict:
1527
return data
1628
except ValueError as exp:
1729
print(exp)
18-
sys.exit(exp)
30+
raise JSONReadError
1931
except IOError as exp:
2032
print(exp)
21-
sys.exit(exp)
33+
raise JSONReadError("Invalid JSON File")
2234

2335

2436
def readfromurl(url: str, params: dict = None) -> dict:
@@ -29,21 +41,21 @@ def readfromurl(url: str, params: dict = None) -> dict:
2941
if response.status_code == 200:
3042
data = response.json()
3143
return data
32-
sys.exit(response.text)
44+
raise URLReadError("URL is not returning correct response")
3345

3446

3547
def readfromstring(jsondata: str) -> dict:
3648
"""
3749
Loads json from string
3850
"""
3951
if not isinstance(jsondata, str):
40-
sys.exit("the input doesn't seem to be valid string")
52+
raise StringReadError("Sorry! the string doesn't seems to a proper JSON")
4153
try:
4254
data = json.loads(jsondata)
4355
except ValueError as exp:
4456
print(exp)
45-
sys.exit(exp)
57+
raise StringReadError("Sorry! the string doesn't seems to a proper JSON")
4658
except Exception as exp:
4759
print(exp)
48-
sys.exit(exp)
60+
raise StringReadError("Sorry! the string doesn't seems to a proper JSON")
4961
return data

tests/test_json2xml.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import xmltodict
1111

1212
from json2xml import json2xml
13-
from json2xml.utils import readfromjson, readfromstring, readfromurl
13+
from json2xml.utils import readfromjson, readfromstring, readfromurl, JSONReadError, StringReadError, URLReadError
1414

1515

1616
class TestJson2xml(unittest.TestCase):
@@ -29,22 +29,18 @@ def test_read_from_json(self):
2929

3030
def test_read_from_invalid_json(self):
3131
"""Test something."""
32-
with pytest.raises(SystemExit) as pytest_wrapped_e:
32+
with pytest.raises(JSONReadError) as pytest_wrapped_e:
3333
data = readfromjson("examples/licht_wrong.json")
34-
assert pytest_wrapped_e.type == SystemExit
34+
assert pytest_wrapped_e.type == JSONReadError
3535

3636
def test_read_from_url(self):
3737
data = readfromurl("https://coderwall.com/vinitcool76.json")
3838
assert type(data) is dict
3939

4040
def test_read_from_wrong_url(self):
41-
"""
42-
Use wrong url and check if there is a sytemExit
43-
"""
44-
with pytest.raises(SystemExit) as pytest_wrapped_e:
41+
with pytest.raises(URLReadError) as pytest_wrapped_e:
4542
data = readfromurl("https://coderwall.com/vinitcool76.jsoni")
46-
print("type is ", pytest_wrapped_e.type)
47-
assert pytest_wrapped_e.type == SystemExit
43+
assert pytest_wrapped_e.type == URLReadError
4844

4945
def test_read_from_jsonstring(self):
5046
data = readfromstring(
@@ -53,11 +49,11 @@ def test_read_from_jsonstring(self):
5349
assert type(data) is dict
5450

5551
def test_read_from_invalid_jsonstring(self):
56-
with pytest.raises(SystemExit) as pytest_wrapped_e:
52+
with pytest.raises(StringReadError) as pytest_wrapped_e:
5753
data = readfromstring(
5854
'{"login":"mojombo","id":1,"avatar_url":"https://avatars0.githubusercontent.com/u/1?v=4"'
5955
)
60-
assert pytest_wrapped_e.type == SystemExit
56+
assert pytest_wrapped_e.type == StringReadError
6157

6258
def test_json_to_xml_conversion(self):
6359
data = readfromstring(

0 commit comments

Comments
 (0)