about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi <fredmanglis@gmail.com>2026-11-12 15:52:10 +0000
committerFrederick Muriuki Muriithi <fredmanglis@gmail.com>2026-11-12 10:16:08 -0600
commit0dc621b8bec44fe91cbfe804e45d8f8136766021 (patch)
tree1fa9270bb2338dee6373aa7db6b0a50fdbd96677
parent3803578a1916338e9ed6255227eb57f52b7bd5a8 (diff)
downloadgn-uploader-0dc621b8bec44fe91cbfe804e45d8f8136766021.tar.gz
Add helpers and handlers for genotype job errors
Add `job_errors.errors_from_job`, which normalises the structured errors a
genotype job records in its stdout (collector `FileStats.errors` and QC result
errors), and collector/QC error handler functions that render them. The HTML
template and job-metadata registration follow in separate commits.

Co-authored-by: pi coding agent <pi@pi.dev>
-rw-r--r--uploader/genotypes/job_errors.py41
-rw-r--r--uploader/genotypes/views.py21
2 files changed, 62 insertions, 0 deletions
diff --git a/uploader/genotypes/job_errors.py b/uploader/genotypes/job_errors.py
new file mode 100644
index 0000000..f4ac4ad
--- /dev/null
+++ b/uploader/genotypes/job_errors.py
@@ -0,0 +1,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))
diff --git a/uploader/genotypes/views.py b/uploader/genotypes/views.py
index 9928532..956e4e0 100644
--- a/uploader/genotypes/views.py
+++ b/uploader/genotypes/views.py
@@ -39,6 +39,7 @@ from r_qtl.utils import parse_missing_values_symbols
 
 from . import collector_config
 from . import collector_job
+from . import job_errors
 from .models import (genotype_markers,
                      genotype_records,
                      genotype_dataset,
@@ -70,6 +71,26 @@ def genotype_qc_success_handler(job):
         "background-jobs.job_summary", job_id=job["job_id"]))
 
 
+def _render_job_errors(job):
+    """Render the structured errors recorded by a genotype job."""
+    errors = job_errors.errors_from_job(job)
+    return render_template(
+        "genotypes/job-errors.html",
+        job=job,
+        errors=errors,
+        structured=bool(errors))
+
+
+def collect_genotype_stats_error_handler(job):
+    """Handle errors from the genotype file statistics collection job."""
+    return _render_job_errors(job)
+
+
+def genotype_qc_error_handler(job):
+    """Handle errors from the genotype file QC job."""
+    return _render_job_errors(job)
+
+
 @genotypesbp.route(
     "/<int:species_id>/populations/<int:population_id>/genotypes/datasets/"
     "<int:dataset_id>/study/<uuid:job_id>",