From 8b8c1551b2c7ae7d773d2bb57d2ba889e99c14d7 Mon Sep 17 00:00:00 2001 From: C1-BA-B1-F3 Date: Sat, 3 Oct 2026 16:18:25 +0800 Subject: [PATCH] fix(core): fall back to raw XML when pretty-printing fails minidom.parseString raises ExpatError on XML with unescaped special characters (e.g. an '&' in a creation date), crashing the whole metadata generation. Catch the error, log it, and return the raw XML so the user still gets usable output. Fixes #336 --- pygeometa/core.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/pygeometa/core.py b/pygeometa/core.py index faf3fc21..57751c55 100644 --- a/pygeometa/core.py +++ b/pygeometa/core.py @@ -413,8 +413,12 @@ def pretty_print(xml: str) -> str: """ LOGGER.debug('pretty-printing XML') - val = minidom.parseString(xml) - return '\n'.join([val for val in val.toprettyxml(indent=' '*2).split('\n') if val.strip()]) # noqa + try: + val = minidom.parseString(xml) + return '\n'.join([val for val in val.toprettyxml(indent=' '*2).split('\n') if val.strip()]) # noqa + except Exception as err: + LOGGER.warning(f'pretty-printing failed: {err}; returning raw XML') + return xml def render_j2_template(mcf: dict, template_dir: str = None) -> str: