From 0f88889a8be2f5596f4d082e1ce8c31de8fc0a64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Sun, 3 Oct 2021 00:19:38 +0200 Subject: [PATCH] Ensure to report the right ID for XML parsing error The XML ID must be the first attribute to update and it should be cleared if we fail to get it. issue10768 review360141002 --- trytond/convert.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/trytond/convert.py b/trytond/convert.py index 7fbc27d4bb..8da210c27b 100644 --- a/trytond/convert.py +++ b/trytond/convert.py @@ -48,7 +48,11 @@ class MenuitemTagHandler: values = {} - self.xml_id = attributes['id'] + try: + self.xml_id = attributes['id'] + except KeyError: + self.xml_id = None + raise for attr in ('name', 'sequence', 'parent', 'action', 'groups'): if attr in attributes: @@ -182,11 +186,14 @@ class RecordTagHandler: # Manage the top level tag if name == "record": + try: + self.xml_id = attributes["id"] + except KeyError: + self.xml_id = None + raise + self.model = self.mh.pool.get(attributes["model"]) - assert self.model, ("The model %s does not exist !" - % (attributes["model"],)) - self.xml_id = attributes["id"] self.update = bool(int(attributes.get('update', '0'))) # create/update a dict containing fields values @@ -300,9 +307,7 @@ class RecordTagHandler: raise Exception("Unexpected closing tag '%s'" % (name,)) def current_state(self): - return "In tag record model %s with id %s.%s." % \ - (self.model and self.model.__name__ or "?", - self.mh.module, self.xml_id) + return "Tag record with id %s.%s." % (self.mh.module, self.xml_id) # Custom exception: @@ -454,7 +459,7 @@ class TrytondXmlHandler(sax.handler.ContentHandler): try: self.sax_parser.parse(source) except Exception as e: - raise Exception("Error " + self.current_state()) from e + raise Exception("Error in " + self.current_state()) from e return self.to_delete def startElement(self, name, attributes): @@ -515,7 +520,7 @@ class TrytondXmlHandler(sax.handler.ContentHandler): if self.taghandler: return self.taghandler.current_state() else: - return '' + return '?' def get_id(self, xml_id):