Skip to content

Commit bd6b0d0

Browse files
committed
Excpetion handling when loading json file from cli
1 parent 14b9dd2 commit bd6b0d0

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

jsonsubschema/_utils.py

+14
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import numbers
1111
import re
1212
import sys
13+
import json
1314

1415
import jsonschema
1516
import intervals as I
@@ -332,3 +333,16 @@ def are_intervals_mergable(i1, i2):
332333
return i1.overlaps(i2) \
333334
or (is_num(i1.lower) and is_num(i2.upper) and i1.lower - i2.upper == 1) \
334335
or (is_num(i2.lower) and is_num(i1.upper) and i2.lower - i1.upper == 1)
336+
337+
338+
def load_json_file(path, msg=None):
339+
with open(path, "r") as fh:
340+
try:
341+
return json.load(fh)
342+
except Exception as e:
343+
if msg:
344+
print(msg, e)
345+
sys.exit(1)
346+
else:
347+
print(e)
348+
sys.exit(1)

jsonsubschema/cli.py

+3-8
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
@author: Andrew Habib
44
'''
55

6-
import json
7-
import jsonref
86
import sys
97

8+
from jsonsubschema._utils import load_json_file
109
from jsonsubschema.api import isSubschema
1110

1211

@@ -18,12 +17,8 @@ def main():
1817
s1_file = sys.argv[1]
1918
s2_file = sys.argv[2]
2019

21-
with open(s1_file, 'r') as f1:
22-
s1 = json.load(f1)
23-
# s1 = jsonref.load(f1)
24-
with open(s2_file, 'r') as f2:
25-
s2 = json.load(f2)
26-
# s2 = jsonref.load(f2)
20+
s1 = load_json_file(s1_file, "LHS file:")
21+
s2 = load_json_file(s2_file, "RHS file:")
2722

2823
print("LHS <: RHS", isSubschema(s1, s2))
2924
print("RHS <: LHS", isSubschema(s2, s1))

0 commit comments

Comments
 (0)