From d8c93bfafe9ca3a8ef8bf6e2debe47c13adf18fb Mon Sep 17 00:00:00 2001
From: Daniel <d.hornung@indiscale.com>
Date: Tue, 7 May 2024 14:26:11 +0200
Subject: [PATCH] ENH: Pretty printing of errors.

---
 .../table_json_conversion/convert.py               | 13 ++++++++++++-
 unittests/table_json_conversion/test_read_data.py  | 14 ++++++++++++++
 2 files changed, 26 insertions(+), 1 deletion(-)

diff --git a/src/caosadvancedtools/table_json_conversion/convert.py b/src/caosadvancedtools/table_json_conversion/convert.py
index 5cc6fe24..acc0cb22 100644
--- a/src/caosadvancedtools/table_json_conversion/convert.py
+++ b/src/caosadvancedtools/table_json_conversion/convert.py
@@ -97,7 +97,9 @@ validate: bool, optional
   If True, validate the result against the schema.
 
 collect_errors: bool, optional
-  If True, do not fail at the first error, but try to collect as many errors as possible.
+  If True, do not fail at the first error, but try to collect as many errors as possible.  After an
+  Exception is raised, the errors can be collected with ``get_errors()`` and printed with
+  ``get_error_str()``.
 
 Returns
 -------
@@ -120,6 +122,15 @@ out: dict
         """Return a dict with collected errors."""
         return self._errors
 
+    def get_error_str(self) -> str:
+        """Return a beautiful string with the collected errors."""
+        result = ""
+        for loc, value in self._errors.items():
+            result += f"Sheet: {loc[0]}\tRow: {loc[1] + 1}\n"
+            for item in value:
+                result += f"\t\t{item[:-1]}:\t{item[-1]}\n"
+        return result
+
     def _check_columns(self, fail_fast: bool = False):
         """Check if the columns correspond to the schema."""
         def missing(path):
diff --git a/unittests/table_json_conversion/test_read_data.py b/unittests/table_json_conversion/test_read_data.py
index 3f4350b6..0eec2e9c 100644
--- a/unittests/table_json_conversion/test_read_data.py
+++ b/unittests/table_json_conversion/test_read_data.py
@@ -142,6 +142,20 @@ def test_faulty_foreign():
             ['url', None]],
     }
 
+    error_str = converter.get_error_str()
+    assert error_str == """Sheet: Training.Organisation.Person\tRow: 9
+\t\t['name']:\tWorld Training Organization 2
+Sheet: Training.Organisation.Person\tRow: 10
+\t\t['date']:\t2024-03-21T14:12:00.000Z
+\t\t['url']:\twww.getlinkahead.com
+Sheet: Training.participant\tRow: 7
+\t\t['date']:\t2024-03-21T14:12:00.000Z
+\t\t['url']:\tNone
+Sheet: Training.participant\tRow: 8
+\t\t['date']:\t2024-03-21T14:12:00.000Z
+\t\t['url']:\tNone
+"""
+
 
 def test_set_in_nested():
     """Test the ``_set_in_nested`` function."""
-- 
GitLab