From 119a946492e7a3233d439176cc34cdf0ad16dc3f Mon Sep 17 00:00:00 2001 From: Jacob C <44846403+chusloj@users.noreply.github.com> Date: Tue, 5 Jan 2021 10:15:14 -0500 Subject: [PATCH] new error messages for missing file and incorrect JSON formatting --- taxcalc/parameters.py | 10 ++++++---- taxcalc/tests/test_calculator.py | 6 +++--- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/taxcalc/parameters.py b/taxcalc/parameters.py index 0db10d96a..fc4f919d7 100644 --- a/taxcalc/parameters.py +++ b/taxcalc/parameters.py @@ -693,16 +693,18 @@ def convert_year_to_int(syr_dict): txt = req.text else: if isinstance(topkey, str) and (topkey == ''): - raise ValueError("String is empty.") + raise ValueError("topkey string is empty.") if isinstance(obj, str): if obj == '': - raise ValueError("String is empty.") + raise ValueError("obj string is empty.") + elif obj.endswith('.json') and not os.path.isfile(obj): + raise FileNotFoundError("The .json file does not exist.") elif ("{" and "}") in obj: txt = obj else: - raise OSError("The .json file or variable does not exist or is misspecified.") + raise ValueError("The JSON variable is misspecified.") else: - raise OSError("The .json file or variable does not exist or is misspecified.") + raise ValueError("The JSON variable is misspecified.") # strip out //-comments without changing line numbers json_txt = re.sub('//.*', ' ', txt) # convert JSON text into a Python dictionary diff --git a/taxcalc/tests/test_calculator.py b/taxcalc/tests/test_calculator.py index b6d822a50..53d7e0610 100644 --- a/taxcalc/tests/test_calculator.py +++ b/taxcalc/tests/test_calculator.py @@ -504,7 +504,7 @@ def test_read_bad_json_assump_file(): """ with pytest.raises(ValueError): Calculator.read_json_param_objects(None, badassump1) - with pytest.raises(OSError): + with pytest.raises(ValueError): Calculator.read_json_param_objects(None, 'unknown_file_name') with pytest.raises(ValueError): Calculator.read_json_param_objects(None, list()) @@ -514,9 +514,9 @@ def test_json_doesnt_exist(): """ Test JSON file which doesn't exist """ - with pytest.raises(OSError): + with pytest.raises(FileNotFoundError): Calculator.read_json_param_objects(None, './reforms/doesnt_exist.json') - with pytest.raises(OSError): + with pytest.raises(FileNotFoundError): Calculator.read_json_param_objects('./reforms/doesnt_exist.json', None)