Skip to content

Commit

Permalink
Improve error handling of from xml conversions, both in GMSO and ffutils
Browse files Browse the repository at this point in the history
  • Loading branch information
CalCraven committed Sep 5, 2023
1 parent bc900cb commit e19413c
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 8 deletions.
3 changes: 2 additions & 1 deletion gmso/core/forcefield.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from typing import Iterable

from lxml import etree
from pydantic import ValidationError

from gmso.core.element import element_by_symbol
from gmso.exceptions import (
Expand Down Expand Up @@ -573,7 +574,7 @@ def xml_from_forcefield_utilities(cls, filename):
try:
loader = GMSOFFs()
ff = loader.load(filename).to_gmso_ff()
except (ForceFieldParseError, FileNotFoundError):
except (ForceFieldParseError, FileNotFoundError, ValidationError):
loader = FoyerFFs()
ff = loader.load(filename).to_gmso_ff()
return ff
Expand Down
2 changes: 1 addition & 1 deletion gmso/external/convert_foyer_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


@deprecate_function(
"Converting directly to GMSO XML from Foyer XML is deprecated."
"The `from_foyer_xml` method will be deprecated soon. Please use the package `forcefield-utilities.FoyerFFs`."
)
def from_foyer_xml(
foyer_xml, gmso_xml=None, overwrite=False, validate_foyer=False
Expand Down
6 changes: 5 additions & 1 deletion gmso/tests/test_xml_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ def test_load_write_xmls_ffutils_backend(self, xml):

def test_xml_error_handling(self):
"""Validate bad xml formatting in xmls."""
pass
file_path = "dummy_name.xml"
with pytest.raises(FileNotFoundError):
ForceField(file_path)
with pytest.raises(IndexError):
ForceField(get_path("empty_foyer.xml"))

def test_kb_in_ffutils(self):
xml_path = get_path("ff-example0.xml")
Expand Down
2 changes: 1 addition & 1 deletion gmso/utils/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def _inner(*args, **kwargs):


def deprecate_function(msg, klass=PendingDeprecationWarning):
"""Raise a warning that a given function is deprecated."""
"""Raise a warning that a given function will be deprecated soon."""

def decorator(func):
@functools.wraps(func)
Expand Down
23 changes: 19 additions & 4 deletions gmso/utils/ff_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,25 @@ def _validate_schema(xml_path_or_etree, schema=None):
ff_xml = etree.parse(xml_path_or_etree)
try:
xml_schema.assertValid(ff_xml)
except:
raise ForceFieldParseError(
f"xml scheme {xml_schema} has invalid elements. Check the sections of the file."
)
except etree.DocumentInvalid as ex:
message = ex.error_log.last_error.message
line = ex.error_log.last_error.line
# rewrite error message for constraint violation
if ex.error_log.last_error.type_name == "SCHEMAV_CVC_IDC":
for keyword in error_texts:
if keyword in message:
atomtype = message[

Check warning on line 324 in gmso/utils/ff_utils.py

View check run for this annotation

Codecov / codecov/patch

gmso/utils/ff_utils.py#L322-L324

Added lines #L322 - L324 were not covered by tests
message.find("[") + 1 : message.find("]")
]
error_text = error_texts[keyword].format(atomtype, line)
raise ForceFieldParseError((error_text, ex, line))

Check warning on line 328 in gmso/utils/ff_utils.py

View check run for this annotation

Codecov / codecov/patch

gmso/utils/ff_utils.py#L327-L328

Added lines #L327 - L328 were not covered by tests
else:
raise ForceFieldParseError(

Check warning on line 330 in gmso/utils/ff_utils.py

View check run for this annotation

Codecov / codecov/patch

gmso/utils/ff_utils.py#L330

Added line #L330 was not covered by tests
"Unhandled XML validation error. "
"Please consider submitting a bug report.",
ex,
line,
)
return ff_xml


Expand Down

0 comments on commit e19413c

Please sign in to comment.