mitai-jinkendo/backend/csv_parser/import_errors.py
Lars 0629f88b37
All checks were successful
Deploy Development / deploy (push) Successful in 55s
Build Test / pytest-backend (push) Successful in 4s
Build Test / lint-backend (push) Successful in 0s
Build Test / build-frontend (push) Successful in 17s
feat(csv-templates): Add CSV template validation endpoint and enhance error handling
- Introduced a new endpoint for validating CSV templates without saving, allowing users to check field mappings and type conversions.
- Updated the `create_system_template` and `update_system_template` functions to include validation reports in responses.
- Enhanced error handling in CSV import processes by integrating `enrich_row_error` for more informative error messages.
- Improved the AdminCsvTemplateEditorPage to support format checking and display validation results, enhancing user experience.
- Incremented version numbers for `csv_import` and `admin_csv_templates` to reflect these updates.
2026-04-11 06:47:27 +02:00

54 lines
2.2 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
Menschenlesbare Hinweise zu typischen Import-/DB-Fehlern (Universal-CSV).
"""
from __future__ import annotations
def enrich_row_error(message: str, module: str | None = None) -> dict[str, str | None]:
"""
Ergänzt eine Rohexception-Zeichenkette um ``code`` und ``hint`` für die Fehlerliste im Import.
"""
low = (message or "").lower()
out: dict[str, str | None] = {"error": message, "code": None, "hint": None}
if "numeric field overflow" in low or "numeric value out of range" in low:
out["code"] = "db_numeric_overflow"
out["hint"] = (
"Wert passt nicht in die Datenbank-Spalte (z. B. NUMERIC mit begrenzter Größe). "
"Häufig: Kilojoule aus dem Export landen im Kalorien-Feld in der Vorlage für kcal_active/kcal_resting "
'"source_unit": "kj" setzen. Oder eine falsche CSV-Spalte ist einem kleinen Zielfeld zugeordnet '
"(z. B. große Zahl in einem HF-Feld)."
)
return out
if "violates check constraint" in low and "source" in low:
out["code"] = "db_check_constraint_source"
out["hint"] = (
"Die Tabelle erlaubt den gesetzten «source»-Wert nicht. "
"System-Vorlage / Migration zur erlaubten Quelle prüfen (z. B. csv für Universal-Import)."
)
return out
if "current transaction is aborted" in low:
out["code"] = "transaction_aborted"
out["hint"] = (
"Eine frühere Zeile hat einen Datenbankfehler ausgelöst. "
"Zuerst die niedrigste Zeilennummer in error_details beheben (Vorlage/Daten prüfen)."
)
return out
if "invalid input syntax" in low and "time" in low:
out["code"] = "db_time_cast"
out["hint"] = (
"start_time/end_time passen nicht zum erwarteten Zeitformat in der Datenbank. "
"Vorlage: Datums- und Zeitanteil konsistent (oft nur Uhrzeit, wenn date separat)."
)
return out
if module == "activity" and "foreign key" in low:
out["code"] = "db_foreign_key"
out["hint"] = "Verknüpfung zur Datenbank verletzt (z. B. training_type). Support kontaktieren."
return out