Skip to content

Commit 0b3f828

Browse files
Merge pull request #135 from NessieCanCode/replace-return-with-raise-for-exceptions
2 parents c361aa1 + 6d887c8 commit 0b3f828

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/invoice.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@ def _load_profile(base_dir):
4343
return {}
4444
except json.JSONDecodeError as exc:
4545
logging.error("Failed to parse %s: %s", path, exc)
46+
raise
4647
except OSError as exc:
4748
logging.error("Unable to read %s: %s", path, exc)
48-
return {}
49+
raise
4950

5051

5152
def _profile_sections(profile):
@@ -198,7 +199,15 @@ def generate_invoice(buffer, invoice_data):
198199

199200
def main():
200201
base_dir = os.path.dirname(os.path.abspath(__file__))
201-
profile = _load_profile(base_dir)
202+
try:
203+
profile = _load_profile(base_dir)
204+
except json.JSONDecodeError as exc:
205+
print(f"Invalid institution profile: {exc}", file=sys.stderr)
206+
sys.exit(1)
207+
except OSError as exc:
208+
print(f"Unable to read institution profile: {exc}", file=sys.stderr)
209+
sys.exit(1)
210+
202211
invoice_data = json.load(sys.stdin)
203212

204213
# Fill in profile-based sections if not provided

test/unit/profile_loading.test.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import unittest
44
from unittest import mock
55
import logging
6+
import json
67

78
from invoice import _load_profile
89

@@ -18,14 +19,16 @@ def test_invalid_json_logs_error(self):
1819
with open(path, "w", encoding="utf-8") as fh:
1920
fh.write("{invalid")
2021
with self.assertLogs(level="ERROR") as cm:
21-
self.assertEqual(_load_profile(td), {})
22+
with self.assertRaises(json.JSONDecodeError):
23+
_load_profile(td)
2224
self.assertIn("Failed to parse", cm.output[0])
2325

2426
def test_os_error_logs_error(self):
2527
with tempfile.TemporaryDirectory() as td:
2628
with mock.patch("builtins.open", side_effect=PermissionError("denied")):
2729
with self.assertLogs(level="ERROR") as cm:
28-
self.assertEqual(_load_profile(td), {})
30+
with self.assertRaises(OSError):
31+
_load_profile(td)
2932
self.assertIn("Unable to read", cm.output[0])
3033

3134

0 commit comments

Comments
 (0)