Skip to content

Commit 57596aa

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

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

l10n_it_fatturapa_out/tests/test_fatturapa_xml_validation.py

Lines changed: 52 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,56 @@ 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 address or city
894+
895+
expect to fail with a proper message
896+
"""
897+
invoice = self._create_invoice()
898+
invoice.partner_id.street = False
899+
invoice.partner_id.city = False
900+
invoice._post()
901+
wizard = self.wizard_model.create({})
902+
with self.assertRaises(UserError) as ue:
903+
wizard.with_context({"active_ids": [invoice.id]}).exportFatturaPA()
904+
error_msg = ue.exception.args[0]
905+
error_fragments = (
906+
f"Error processing invoice(s) {invoice.name}",
907+
"Indirizzo",
908+
"Comune",
909+
"Activate debug mode to see the full error",
910+
)
911+
for fragment in error_fragments:
912+
self.assertIn(fragment, error_msg)
913+
914+
try:
915+
# Enter debug mode and add details
916+
odoo.http._request_stack.push(
917+
Mock(
918+
db=self.env.cr.dbname,
919+
env=self.env,
920+
debug=True,
921+
website=False, # compatibility with website module
922+
is_frontend=False,
923+
)
924+
)
925+
wizard = self.wizard_model.create({})
926+
with self.assertRaises(UserError) as ue:
927+
wizard.with_context({"active_ids": [invoice.id]}).exportFatturaPA()
928+
debug_error_msg = ue.exception.args[0]
929+
debug_error_fragments = (
930+
"Full error follows",
931+
"Reason: value doesn't match any pattern of",
932+
"p{IsBasicLatin}",
933+
"<Comune xmlns:ns1",
934+
)
935+
for fragment in error_fragments[:-1] + debug_error_fragments:
936+
self.assertIn(fragment, debug_error_msg)
937+
finally:
938+
# Remove from the stack to not interfere with other tests
939+
odoo.http._request_stack.pop()
940+
889941
def test_multicompany_fail(self):
890942
"""
891943
- 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)