Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions test/unit/profile_loading.test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import unittest
from unittest import mock
import logging
import json

from invoice import _load_profile

Expand All @@ -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])


Expand Down
Loading