]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virXMLParseHelper: Rework error reporting
authorPeter Krempa <pkrempa@redhat.com>
Wed, 14 Apr 2021 11:03:58 +0000 (13:03 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Fri, 16 Apr 2021 11:17:35 +0000 (13:17 +0200)
Move the reporting of parsing error on the error path of the parser as
other code paths report their own errors already.

Additionally prefer printing the 'url' as document name if provided
instead of "[inline data]" as that usually gives a better hint at least
which kind of XML is being parsed.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Tim Wiederhake <twiederh@redhat.com>
src/util/virxml.c

index 117f50f2bf4ac71fbc136b77ff46af4285164c5c..029b3d646eb504b0e9c40a597c5f20c050cc8ecf 100644 (file)
@@ -788,6 +788,14 @@ virXMLParseHelper(int domcode,
     struct virParserData private;
     xmlParserCtxtPtr pctxt;
     xmlDocPtr xml = NULL;
+    const char *docname;
+
+    if (filename)
+        docname = filename;
+    else if (url)
+        docname = url;
+    else
+        docname = "[inline data]";
 
     /* Set up a parser context so we can catch the details of XML errors. */
     pctxt = xmlNewParserCtxt();
@@ -807,8 +815,16 @@ virXMLParseHelper(int domcode,
                              XML_PARSE_NONET |
                              XML_PARSE_NOWARNING);
     }
-    if (!xml)
+
+    if (!xml) {
+        if (virGetLastErrorCode() == VIR_ERR_OK) {
+            virGenericReportError(domcode, VIR_ERR_XML_ERROR,
+                                  _("failed to parse xml document '%s'"),
+                                  docname);
+        }
+
         goto error;
+    }
 
     if (xmlDocGetRootElement(xml) == NULL) {
         virGenericReportError(domcode, VIR_ERR_INTERNAL_ERROR,
@@ -832,11 +848,6 @@ virXMLParseHelper(int domcode,
     xmlFreeDoc(xml);
     xml = NULL;
 
-    if (virGetLastErrorCode() == VIR_ERR_OK) {
-        virGenericReportError(domcode, VIR_ERR_XML_ERROR,
-                              _("failed to parse xml document '%s'"),
-                              filename ? filename : "[inline data]");
-    }
     goto cleanup;
 }