virXMLParseHelper: Rework error reporting

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>
This commit is contained in:
Peter Krempa 2021-04-14 13:03:58 +02:00
parent 5339ecf6b9
commit e87eeefb3e

View 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;
}