Open nuntius35 opened 1 week ago
Hi, the documentation says that the base exception "let you catch all the errors generated by the library". This case is on the boundary because this a ParseError of the ElementTree library.
Anyway it's better to reason on that because other errors are caught and re-raised.
In this case the error is in the syntax of the XML source, so it should raise an XMLResourceError or a derived exception from it. For v4.0 the XMLResource will be extended to support also parsing with lxml and custom url openers. All the XML data access in the library is delegated to this class so having its error type hierarchy could help to distinguish between XML data access/parsing and XML validation.
An hypothesis for this could be:
from xml.etree.ElementTree import ParseError
from xmlschema import XMLSchemaException
class XMLResourceError(XMLSchemaException):
"""A generic error on an XML resource that let you catch all the errors generated by an XML resource."""
class XMLResourceOSError(XMLResourceError, OSError):
"""Raised when an error is found accessing an XML resource."""
class XMLResourceParseError(XMLResourceError, ParseError):
"""Raised when an error is found parsing an XML resource."""
class XMLResourceBlocked(XMLResourceError):
"""Raised when an XML resource access is blocked by security settings."""
class XMLResourceForbidden(XMLResourceError):
"""Raised when the parsing of an XML resource is forbidden for safety reasons."""
Deriving XMLResourceParseError
from ParseError
instead of SyntaxError
preserves backward compatibility.
There are errors in an xsd file that raise an
xml.etree.ElementTree.ParseError
and do not cause anXMLSchemaException
. The documentation ofXMLSchemaException
claims that it catches all exceptions.Example
File:
invalid_schema.xsd
File:
validate_schema.py
Run the python script with
python validate_schema.py
.Expected behaviour:
XMLSchema11
raises e.g.XMLSchemaValidatorError
and the exception is caught.Actual behaviour: the exception
xml.etree.ElementTree.ParseError
is raised and not caught.