about summary refs log tree commit diff
path: root/uploader/genotypes/job_errors.py
blob: f4ac4addaa40cb1ac667f63be0558a1a62a45999 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"""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))