Skip to content

Commit cdb0d24

Browse files
committed
Improved the error reporting for invalid xml file submissions.
- Legacy-Id: 9916
1 parent cfb7dc3 commit cdb0d24

1 file changed

Lines changed: 19 additions & 3 deletions

File tree

ietf/submit/forms.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ def clean(self):
136136
if self.cleaned_data.get('xml'):
137137
#if not self.cleaned_data.get('txt'):
138138
xml_file = self.cleaned_data.get('xml')
139-
tfh, tfn = tempfile.mkstemp(suffix='.xml')
139+
name, ext = os.path.splitext(os.path.basename(xml_file.name))
140+
tfh, tfn = tempfile.mkstemp(prefix=name+'-', suffix='.xml')
140141
try:
141142
# We need to write the xml file to disk in order to hand it
142143
# over to the xml parser. XXX FIXME: investigate updating
@@ -149,8 +150,21 @@ def clean(self):
149150
parser = xml2rfc.XmlRfcParser(tfn, quiet=True)
150151
self.xmltree = parser.parse()
151152
ok, errors = self.xmltree.validate()
153+
debug.type('errors')
152154
if not ok:
153-
raise forms.ValidationError(errors)
155+
# Each error has properties:
156+
#
157+
# message: the message text
158+
# domain: the domain ID (see lxml.etree.ErrorDomains)
159+
# type: the message type ID (see lxml.etree.ErrorTypes)
160+
# level: the log level ID (see lxml.etree.ErrorLevels)
161+
# line: the line at which the message originated (if applicable)
162+
# column: the character column at which the message originated (if applicable)
163+
# filename: the name of the file in which the message originated (if applicable)
164+
raise forms.ValidationError(
165+
[ forms.ValidationError("One or more XML validation errors occurred when processing the XML file:") ] +
166+
[ forms.ValidationError("%s: Line %s: %s" % (xml_file.name, e.line, e.message), code="%s"%e.type) for e in errors ]
167+
)
154168
self.xmlroot = self.xmltree.getroot()
155169
draftname = self.xmlroot.attrib.get('docName')
156170
revmatch = re.search("-[0-9][0-9]$", draftname)
@@ -174,8 +188,10 @@ def clean(self):
174188
self.author_list.append(author_dict)
175189
line = "%(full_name)s <%(email)s>" % author_dict
176190
self.authors.append(line)
191+
except forms.ValidationError:
192+
raise
177193
except Exception as e:
178-
raise forms.ValidationError("Exception: %s" % e)
194+
raise forms.ValidationError("Exception when trying to process the XML file: %s" % e)
179195
finally:
180196
os.close(tfh)
181197
os.unlink(tfn)

0 commit comments

Comments
 (0)