Skip to content

Commit e2684b6

Browse files
committed
[IMP] l10n_it_fatturapa_out: XML validation prints nicer error msg
1 parent 63ec1f5 commit e2684b6

File tree

2 files changed

+99
-1
lines changed

2 files changed

+99
-1
lines changed

l10n_it_fatturapa_out/tests/test_fatturapa_xml_validation.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66

77
import base64
88
import re
9+
from unittest.mock import Mock
910

1011
from psycopg2 import IntegrityError
1112

13+
import odoo
1214
from odoo import fields
1315
from odoo.exceptions import UserError
1416
from odoo.tests import Form, tagged
@@ -886,6 +888,83 @@ def test_no_tax_fail(self):
886888
)
887889
self.assertEqual(ue.exception.args[0], error_message)
888890

891+
def test_partner_no_address_fail(self):
892+
"""
893+
- create an XML invoice where the customer has no city or zip
894+
895+
expect to fail with a proper message
896+
"""
897+
self.res_partner_fatturapa_0.street = False
898+
self.res_partner_fatturapa_0.city = False
899+
invoice = self.invoice_model.create(
900+
{
901+
"invoice_date": "2016-06-15",
902+
"partner_id": self.res_partner_fatturapa_0.id,
903+
"journal_id": self.sales_journal.id,
904+
"invoice_payment_term_id": self.account_payment_term.id,
905+
"user_id": self.user_demo.id,
906+
"move_type": "out_invoice",
907+
"currency_id": self.EUR.id,
908+
"invoice_line_ids": [
909+
(
910+
0,
911+
0,
912+
{
913+
"account_id": self.a_sale.id,
914+
"product_id": self.product_product_10.id,
915+
"name": "Mouse, Optical",
916+
"quantity": 1,
917+
"product_uom_id": self.product_uom_unit.id,
918+
"price_unit": 10,
919+
"tax_ids": [(6, 0, {self.tax_22.id})],
920+
},
921+
),
922+
(
923+
0,
924+
0,
925+
{
926+
"account_id": self.a_sale.id,
927+
"product_id": self.product_order_01.id,
928+
"name": "Zed+ Antivirus",
929+
"quantity": 1,
930+
"product_uom_id": self.product_uom_unit.id,
931+
"price_unit": 4,
932+
"tax_ids": [(6, 0, {self.tax_22.id})],
933+
},
934+
),
935+
],
936+
}
937+
)
938+
invoice._post()
939+
wizard = self.wizard_model.create({})
940+
with self.assertRaises(UserError) as ue:
941+
wizard.with_context({"active_ids": [invoice.id]}).exportFatturaPA()
942+
error_msg = ue.exception.args[0]
943+
error_fragments = [
944+
f"Error processing invoice(s) {invoice.name}",
945+
"Indirizzo",
946+
"Comune",
947+
"Activate debug mode to see the full error",
948+
]
949+
for fragment in error_fragments:
950+
self.assertIn(fragment, error_msg)
951+
952+
# Enter debug mode and add details
953+
odoo.http._request_stack.push(
954+
Mock(db=self.env.cr.dbname, env=self.env, debug=True)
955+
)
956+
wizard = self.wizard_model.create({})
957+
with self.assertRaises(UserError) as ue:
958+
wizard.with_context({"active_ids": [invoice.id]}).exportFatturaPA()
959+
debug_error_msg = ue.exception.args[0]
960+
debug_error_fragments = [
961+
"Full error follows",
962+
"Reason: value doesn't match any pattern of",
963+
"class 'lxml.etree._Element'>",
964+
]
965+
for fragment in error_fragments[:-1] + debug_error_fragments:
966+
self.assertIn(fragment, debug_error_msg)
967+
889968
def test_multicompany_fail(self):
890969
"""
891970
- create two invoices in two different companies

l10n_it_fatturapa_out/wizard/efattura.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,26 @@ def to_xml(self, env):
261261
# i controlli precedenti dovrebbero escludere errori di sintassi XML
262262
# with open("/tmp/fatturaout.xml", "wb") as o:
263263
# o.write(etree.tostring(root, xml_declaration=True, encoding="utf-8"))
264-
raise UserError("\n".join(str(e) for e in errors))
264+
# Print error paths, as they can be helpful even for non-technical people
265+
error_path_string = "\n- ".join(
266+
err.path[err.path.index(":") + 1 :] for err in errors
267+
)
268+
error_msg = _(
269+
"Error processing invoice(s) %(invoices)s.\n\n"
270+
"Errors in the following fields:\n- %(error_path_string)s\n\n",
271+
invoices=", ".join(
272+
inv.display_name for inv in template_values["invoices"]
273+
),
274+
error_path_string=error_path_string,
275+
)
276+
# add details in debug mode
277+
if env.user.user_has_groups("base.group_no_one"):
278+
error_msg += _("Full error follows:\n\n") + "\n".join(
279+
str(e) for e in errors
280+
)
281+
else:
282+
error_msg += _("Activate debug mode to see the full error.")
283+
raise UserError(error_msg)
265284
content = etree.tostring(root, xml_declaration=True, encoding="utf-8")
266285
return content
267286

0 commit comments

Comments
 (0)