From 5691f551449c6476a788d30b0709e59bde26c317 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Simon=20K=C3=BCnzel?= <simonk@fsmpi.rwth-aachen.de>
Date: Sat, 5 Oct 2024 00:10:53 +0200
Subject: [PATCH] Add job errors

---
 src/videoag_common/miscellaneous/errors.py |  3 +++
 src/videoag_common/miscellaneous/json.py   |  2 +-
 src/videoag_common/objects/job.py          | 14 ++++++++++++--
 3 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/src/videoag_common/miscellaneous/errors.py b/src/videoag_common/miscellaneous/errors.py
index 0ca718b..6730d46 100644
--- a/src/videoag_common/miscellaneous/errors.py
+++ b/src/videoag_common/miscellaneous/errors.py
@@ -7,6 +7,9 @@ class ApiError:
         self.error_code = error_code
         self.http_status_code = http_status_code
         self.message = message
+    
+    def __str__(self):
+        return f"ApiError[code={self.error_code}, status={self.http_status_code}, message='{self.message}']"
 
 
 class ApiClientException(Exception):
diff --git a/src/videoag_common/miscellaneous/json.py b/src/videoag_common/miscellaneous/json.py
index 6399361..8895662 100644
--- a/src/videoag_common/miscellaneous/json.py
+++ b/src/videoag_common/miscellaneous/json.py
@@ -88,7 +88,7 @@ class CJsonObject(CJsonValue):
         if optional and not self.has(key):
             return None
         if key not in self._data:
-            raise ApiClientException(ERROR_REQUEST_MISSING_PARAMETER(key))
+            raise ApiClientException(ERROR_REQUEST_MISSING_PARAMETER(f"{self._path}.{key}"))
         return CJsonValue(self._data[key], f"{self._path}.{key}")
     
     def get_object(self, key: str) -> "CJsonObject":
diff --git a/src/videoag_common/objects/job.py b/src/videoag_common/objects/job.py
index 5b9e5a1..df45d68 100644
--- a/src/videoag_common/objects/job.py
+++ b/src/videoag_common/objects/job.py
@@ -9,8 +9,8 @@ from .user import User
 
 class JobState(Enum):
     READY = "ready"
-    SPAWNING = "spawning"
-    RUNNING = "running"
+    SPAWNING = "spawning"  # Job is in K8s but wasn't scheduled yet
+    RUNNING = "running"  # Job is running in K8s
     FINISHED = "finished"
     FINISHED_AND_PROCESSED = "finished_and_processed"
     FAILED = "failed"
@@ -22,6 +22,9 @@ _JOB_STATE_ENUM = create_enum_type(JobState)
 
 
 class Job(ApiObject, Base):
+    ERROR_CODE_UNKNOWN_TYPE = "unknown_type"
+    ERROR_CODE_SPAWNING_FAILURE = "spawning_failure"
+    
     __table_args__ = (
         CheckConstraint(
             "(on_end_event_type IS NULL) = (on_end_event_data IS NULL)",
@@ -127,3 +130,10 @@ class Job(ApiObject, Base):
             },
             cause_job_id=cause_job_id
         )
+    
+    def set_error(self, error_code: str, error_message: str or None = None):
+        self.status = JobState.FAILED
+        self.output_data = {
+            "error_code": error_code,
+            "error_message": error_message
+        }
-- 
GitLab