"""Extract structured errors from genotype job output.""" import json from typing import Any, Mapping def _line_text(line: Any) -> str: """Render an error's line context as text.""" if isinstance(line, (list, tuple)): return ", ".join(str(part) for part in line) return "" if line is None else str(line) def _normalise(error: Mapping[str, Any]) -> dict: """Normalise collector and QC error records to one shape.""" return { "row": str(error.get("row_header", error.get("row", ""))), "column": str(error.get("column_header", error.get("column", ""))), "value": str(error.get("value", "")), "message": str(error.get("message", "")), "line": _line_text(error.get("line", "")), } def errors_from_job(job: Mapping[str, Any]) -> tuple[dict, ...]: """Return the structured errors a job recorded in its stdout, if any. Both the collector's `FileStats.errors` and the QC worker's result errors are JSON objects with an ``errors`` list; this normalises them for display. """ try: output = json.loads(job.get("stdout") or "") except (TypeError, ValueError): return tuple() if not isinstance(output, dict): return tuple() errors = output.get("errors") if not isinstance(errors, list): return tuple() return tuple( _normalise(error) for error in errors if isinstance(error, dict))