[PATCH 4/5] virXMLParseHelper: Rework error reporting

Peter Krempa pkrempa at redhat.com
Fri Apr 16 08:07:04 UTC 2021


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 at redhat.com>
---
 src/util/virxml.c | 23 +++++++++++++++++------
 1 file changed, 17 insertions(+), 6 deletions(-)

diff --git a/src/util/virxml.c b/src/util/virxml.c
index 117f50f2bf..029b3d646e 100644
--- a/src/util/virxml.c
+++ b/src/util/virxml.c
@@ -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;
 }

-- 
2.30.2




More information about the libvir-list mailing list