From cdb0d24aeda993e560d8c9fd6f8089a0de6f1f0c Mon Sep 17 00:00:00 2001
From: Henrik Levkowetz <henrik@levkowetz.com>
Date: Thu, 30 Jul 2015 11:44:54 +0000
Subject: [PATCH] Improved the error reporting for invalid xml file
 submissions.  - Legacy-Id: 9916

---
 ietf/submit/forms.py | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)

diff --git a/ietf/submit/forms.py b/ietf/submit/forms.py
index b59b6104f..240fbd828 100644
--- a/ietf/submit/forms.py
+++ b/ietf/submit/forms.py
@@ -136,7 +136,8 @@ class SubmissionUploadForm(forms.Form):
         if self.cleaned_data.get('xml'):
             #if not self.cleaned_data.get('txt'):
             xml_file = self.cleaned_data.get('xml')
-            tfh, tfn = tempfile.mkstemp(suffix='.xml')
+            name, ext = os.path.splitext(os.path.basename(xml_file.name))
+            tfh, tfn = tempfile.mkstemp(prefix=name+'-', suffix='.xml')
             try:
                 # We need to write the xml file to disk in order to hand it
                 # over to the xml parser.  XXX FIXME: investigate updating
@@ -149,8 +150,21 @@ class SubmissionUploadForm(forms.Form):
                 parser = xml2rfc.XmlRfcParser(tfn, quiet=True)
                 self.xmltree = parser.parse()
                 ok, errors = self.xmltree.validate()
+                debug.type('errors')
                 if not ok:
-                    raise forms.ValidationError(errors)
+                    # Each error has properties:
+                    #
+                    #     message:  the message text
+                    #     domain:   the domain ID (see lxml.etree.ErrorDomains)
+                    #     type:     the message type ID (see lxml.etree.ErrorTypes)
+                    #     level:    the log level ID (see lxml.etree.ErrorLevels)
+                    #     line:     the line at which the message originated (if applicable)
+                    #     column:   the character column at which the message originated (if applicable)
+                    #     filename: the name of the file in which the message originated (if applicable)
+                    raise forms.ValidationError(
+                            [ forms.ValidationError("One or more XML validation errors occurred when processing the XML file:") ] +
+                            [ forms.ValidationError("%s: Line %s: %s" % (xml_file.name, e.line, e.message), code="%s"%e.type) for e in errors ]
+                        )
                 self.xmlroot = self.xmltree.getroot()
                 draftname = self.xmlroot.attrib.get('docName')
                 revmatch = re.search("-[0-9][0-9]$", draftname)
@@ -174,8 +188,10 @@ class SubmissionUploadForm(forms.Form):
                     self.author_list.append(author_dict)
                     line = "%(full_name)s <%(email)s>" % author_dict
                     self.authors.append(line)
+            except forms.ValidationError:
+                raise
             except Exception as e:
-                raise forms.ValidationError("Exception: %s" % e)
+                raise forms.ValidationError("Exception when trying to process the XML file: %s" % e)
             finally:
                 os.close(tfh)
                 os.unlink(tfn)