[Libosinfo] [PATCH 3/4] loader: improve error reporting upon libxml2 error

Daniel P. Berrange berrange at redhat.com
Tue Oct 20 13:02:35 UTC 2015


Instead of just printing the libxml2 error string and line
number, actually include the filename so the user knows
where to look for the error!

Signed-off-by: Daniel P. Berrange <berrange at redhat.com>
---
 osinfo/osinfo_loader.c | 21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)

diff --git a/osinfo/osinfo_loader.c b/osinfo/osinfo_loader.c
index 2dc29c4..a5e26ad 100644
--- a/osinfo/osinfo_loader.c
+++ b/osinfo/osinfo_loader.c
@@ -1610,18 +1610,25 @@ static void osinfo_loader_root(OsinfoLoader *loader,
     }
 }
 
+
+struct LibosinfoXMLData {
+    GError **err;
+    const gchar *file;
+};
+
 static void
 catchXMLError(void *ctx, const char *msg ATTRIBUTE_UNUSED, ...)
 {
     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
 
     if (ctxt && ctxt->_private) {
-        GError **err = ctxt->_private;
-        if (!error_is_set(err)) {
-            gchar *xmlmsg = g_strdup_printf("at line %d: %s",
+        struct LibosinfoXMLData *data = ctxt->_private;
+        if (!error_is_set(data->err)) {
+            gchar *xmlmsg = g_strdup_printf("Cannot parse %s at line %d: %s",
+                                            data->file,
                                             ctxt->lastError.line,
                                             ctxt->lastError.message);
-            OSINFO_ERROR(ctxt->_private, xmlmsg);
+            OSINFO_ERROR(data->err, xmlmsg);
             g_free(xmlmsg);
         }
     }
@@ -1637,6 +1644,10 @@ static void osinfo_loader_process_xml(OsinfoLoader *loader,
     xmlXPathContextPtr ctxt = NULL;
     xmlDocPtr xml = NULL;
     xmlNodePtr root;
+    struct LibosinfoXMLData data = {
+        .err = err,
+        .file = src,
+    };
 
     /* Set up a parser context so we can catch the details of XML errors. */
     pctxt = xmlNewParserCtxt();
@@ -1645,7 +1656,7 @@ static void osinfo_loader_process_xml(OsinfoLoader *loader,
         goto cleanup;
     }
 
-    pctxt->_private = err;
+    pctxt->_private = &data;
     pctxt->sax->error = catchXMLError;
 
     xml = xmlCtxtReadDoc(pctxt, BAD_CAST xmlStr, src, NULL,
-- 
2.4.3




More information about the Libosinfo mailing list