Skip to content
Permalink
Browse files

refactor: Improve XML parsing error handling

  • Loading branch information
pawamoy committed Dec 28, 2020
1 parent 2c29211 commit ad864100b644ab1ee8daaa0d3923bc87dee1c5ca
Showing with 13 additions and 7 deletions.
  1. +13 −7 src/mkdocstrings/extension.py
@@ -254,19 +254,25 @@ def log_xml_parse_error(error: str, xml_text: str) -> None:
xml_text: The XML text that generated the parsing error.
"""
message = error
if "mismatched tag" in error:
mismatched_tag = "mismatched tag" in error
undefined_entity = "undefined entity" in error

if mismatched_tag or undefined_entity:
line_column = error[error.rfind(":") + 1 :]
line, column = line_column.split(", ")
lineno = int(line[line.rfind(" ") + 1 :])
columnno = int(column[column.rfind(" ") + 1 :])

line = xml_text.split("\n")[lineno - 1]
character = line[columnno]
message += (
f" (character {character}):\n{line}\n"
"If your Markdown contains angle brackets < >, try to wrap them between backticks `< >`, "
"or replace them with &lt; and &gt;"
)
if mismatched_tag:
character = line[columnno]
message += (
f" (character {character}):\n{line}\n"
"If your Markdown contains angle brackets < >, try to wrap them between backticks `< >`, "
"or replace them with &lt; and &gt;"
)
elif undefined_entity:
message += f":\n{line}\n"
log.error(message)


0 comments on commit ad86410

Please sign in to comment.