From bc5149b025ac4b0e058c319da1c373d3c061ef70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Garc=C3=ADa=20Garz=C3=B3n?= Date: Thu, 28 Dec 2023 06:47:39 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Handling=20NoSuchInvoice=20from=20e?= =?UTF-8?q?rp,=20and=20dummy=20for=20pdf=20errors?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/datasources/dummy.py | 27 +++++++++++++++++++++++++++ backend/datasources/erp.py | 2 ++ backend/datasources/exceptions.py | 3 +++ 3 files changed, 32 insertions(+) diff --git a/backend/datasources/dummy.py b/backend/datasources/dummy.py index 9a47af2..959c4a7 100644 --- a/backend/datasources/dummy.py +++ b/backend/datasources/dummy.py @@ -10,6 +10,7 @@ ContractNotExists, UnauthorizedAccess, NoSuchUser, + NoSuchInvoice, NoDocumentVersions, ) @@ -210,10 +211,31 @@ def dummy_installation_details(username: str, contract_number: str) -> Installat )) return InstallationDetailsResult(**ns.load('frontend/src/data/dummyinstallationdetail.yaml')) +invoice_pdf_exceptions = { + e.__name__: e + for e in [ + UnauthorizedAccess, + NoSuchUser, + NoSuchInvoice, + ErpValidationError, + # TODO: ErpConnectionError, + # TODO: ErpUnexpectedError, + ] +} + def dummy_invoices(username: str) -> list[Invoice]: return [ Invoice(**invoice) for invoice in ns.load('frontend/src/data/dummyinvoices.yaml') + ]+[ + Invoice( + invoice_number=name, + contract_number='10002', + emission_date='2020-01-01', + first_period_date='2020-01-01', + last_period_date='2020-01-01', + amount=200, + ) for name in invoice_pdf_exceptions.keys() ] def pdf_content(invoice_number): @@ -231,6 +253,11 @@ def pdf_content(invoice_number): return output.getvalue() def dummy_invoice_pdf(username: str, invoice_number: str): + if invoice_number in invoice_pdf_exceptions: + raise invoice_pdf_exceptions[invoice_number](dict( + code=invoice_number, + error=f"{invoice_number} (Dummy error)", + )) base64_data = base64.b64encode(Path('/usr/share/doc/tig/manual.pdf').read_bytes()) base64_data = base64.b64encode(pdf_content(invoice_number)) return InvoicePdf( diff --git a/backend/datasources/erp.py b/backend/datasources/erp.py index f414ded..675c13b 100644 --- a/backend/datasources/erp.py +++ b/backend/datasources/erp.py @@ -21,6 +21,7 @@ ContractNotExists, UnauthorizedAccess, NoSuchUser, + NoSuchInvoice, NoDocumentVersions, ) from .dummy import dummy_invoice_pdf @@ -36,6 +37,7 @@ def catch_validation_error(): ContractNotExists, UnauthorizedAccess, NoSuchUser, + NoSuchInvoice, NoDocumentVersions, ] def process_erp_errors(erp_response): diff --git a/backend/datasources/exceptions.py b/backend/datasources/exceptions.py index cfa0923..e636b49 100644 --- a/backend/datasources/exceptions.py +++ b/backend/datasources/exceptions.py @@ -39,5 +39,8 @@ class UnauthorizedAccess(ErpError): class NoSuchUser(ErpError): pass +class NoSuchInvoice(ErpError): + pass + class NoDocumentVersions(ErpError): pass