""" Generate a unified page of documentation for all error conditions. """ import json import os import re from itertools import chain from pathlib import Path from textwrap import dedent # To avoid adding new direct dependencies, we import from within mkdocs. # This is not ideal as unrelated updates to mkdocs may break this script. from mkdocs.structure.pages import markdown THIS_DIR = Path(__file__).parent SPARK_PROJECT_ROOT = THIS_DIR.parents[1] DOCS_ROOT = SPARK_PROJECT_ROOT / "docs" ERROR_CONDITIONS_PATH = ( SPARK_PROJECT_ROOT / "common/utils/src/main/resources/error/error-conditions.json" ) def assemble_message(message_parts): message = " ".join(message_parts) cleaned_message = re.sub(r"(<.*?>)", lambda x: f"`{x.group(1)}`", message) return markdown.markdown(cleaned_message) def load_error_conditions(path): with open(path) as f: raw_error_conditions = json.load(f) error_conditions = dict() for name, details in raw_error_conditions.items(): if name.startswith("_LEGACY_ERROR") or name.startswith("INTERNAL_ERROR"): continue if "subClass" in details: for sub_name in details["subClass"]: details["subClass"][sub_name]["message"] = ( assemble_message(details["subClass"][sub_name]["message"]) ) details["message"] = assemble_message(details["message"]) error_conditions[name] = details return error_conditions def anchor_name(condition_name: str, sub_condition_name: str = None): """ URLs can, in practice, be up to 2,000 characters long without causing any issues. So we preserve the condition name mostly as-is for use in the anchor, even when that name is very long. See: https://stackoverflow.com/a/417184 """ parts = [ part for part in (condition_name, sub_condition_name) if part ] anchor = "-".join(parts).lower().replace("_", "-") return anchor def generate_doc_rows(condition_name, condition_details): condition_row = [ """
#
{condition_name}
#
{sub_condition_name}
Error State / SQLSTATE | Error Condition & Sub-Condition | Message |
---|