From 6d887c82fb3fe1e701db49dcb0d095a2cdee44d0 Mon Sep 17 00:00:00 2001 From: Robert Romero Date: Tue, 9 Sep 2025 16:29:02 -0700 Subject: [PATCH] Raise profile load errors --- src/invoice.py | 13 +++++++++++-- test/unit/profile_loading.test.py | 7 +++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/invoice.py b/src/invoice.py index 8d855ef..e217098 100755 --- a/src/invoice.py +++ b/src/invoice.py @@ -43,9 +43,10 @@ def _load_profile(base_dir): return {} except json.JSONDecodeError as exc: logging.error("Failed to parse %s: %s", path, exc) + raise except OSError as exc: logging.error("Unable to read %s: %s", path, exc) - return {} + raise def _profile_sections(profile): @@ -198,7 +199,15 @@ def generate_invoice(buffer, invoice_data): def main(): base_dir = os.path.dirname(os.path.abspath(__file__)) - profile = _load_profile(base_dir) + try: + profile = _load_profile(base_dir) + except json.JSONDecodeError as exc: + print(f"Invalid institution profile: {exc}", file=sys.stderr) + sys.exit(1) + except OSError as exc: + print(f"Unable to read institution profile: {exc}", file=sys.stderr) + sys.exit(1) + invoice_data = json.load(sys.stdin) # Fill in profile-based sections if not provided diff --git a/test/unit/profile_loading.test.py b/test/unit/profile_loading.test.py index 18e0550..d47ae7e 100644 --- a/test/unit/profile_loading.test.py +++ b/test/unit/profile_loading.test.py @@ -3,6 +3,7 @@ import unittest from unittest import mock import logging +import json from invoice import _load_profile @@ -18,14 +19,16 @@ def test_invalid_json_logs_error(self): with open(path, "w", encoding="utf-8") as fh: fh.write("{invalid") with self.assertLogs(level="ERROR") as cm: - self.assertEqual(_load_profile(td), {}) + with self.assertRaises(json.JSONDecodeError): + _load_profile(td) self.assertIn("Failed to parse", cm.output[0]) def test_os_error_logs_error(self): with tempfile.TemporaryDirectory() as td: with mock.patch("builtins.open", side_effect=PermissionError("denied")): with self.assertLogs(level="ERROR") as cm: - self.assertEqual(_load_profile(td), {}) + with self.assertRaises(OSError): + _load_profile(td) self.assertIn("Unable to read", cm.output[0])