Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XML errors: make file and line more clear #1289

Merged
merged 1 commit into from
Jun 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions dev_tools/nxdl/syntax.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from contextlib import contextmanager
from typing import Optional

import lxml.etree
Expand All @@ -16,13 +17,19 @@ def validate_definition(
xml_schema: Optional[lxml.etree.XMLSchema] = None,
):
xml_file_name = str(xml_file_name)
try:
with _handle_xml_error(xml_file_name, lxml.etree.XMLSyntaxError):
xml_tree = lxml.etree.parse(xml_file_name)
except lxml.etree.XMLSyntaxError:
raise errors.XMLSyntaxError(xml_file_name)
if xml_schema is None:
xml_schema = nxdl_schema()
try:
with _handle_xml_error(xml_file_name, lxml.etree.DocumentInvalid):
xml_schema.assertValid(xml_tree)
except lxml.etree.DocumentInvalid:
raise errors.NXDLSyntaxError(xml_file_name)


@contextmanager
def _handle_xml_error(xml_file_name: str, *exception_types):
try:
yield
except exception_types as e:
raise errors.XMLSyntaxError(
"\n " + "\n ".join([xml_file_name] + str(e).rsplit(":", 1))
) from e