Parcourir la source

fix(module): Capture the abnormal value of configuration items and set it to the default value #859

ryland il y a 1 an
Parent
commit
7cdef0ac44
1 fichiers modifiés avec 31 ajouts et 2 suppressions
  1. 31 2
      tasks/Component/config_base.py

+ 31 - 2
tasks/Component/config_base.py

@@ -5,7 +5,7 @@ import re
 from datetime import timedelta, time, datetime
 from typing import Any
 
-from pydantic import BaseModel
+from pydantic import BaseModel, ValidationError
 from pydantic import (BeforeValidator,
                       PlainSerializer,
                       WithJsonSchema,
@@ -80,4 +80,33 @@ def dynamic_hide(*fields: str,):
 
 
 class ConfigBase(BaseModel):
-    pass
+    def __init__(self, *args, **kwargs):
+        try:
+            super().__init__(*args, **kwargs)
+        except ValidationError as exc:
+            """
+            During the initialization of the ConfigBase class, 
+            if a ValidationError occurs, the default value of the field is used to initialize the field, 
+            and the exception is re-raised after the initialization is complete.
+            """
+            exc_info = exc.errors()[0]
+            val_error_type = exc_info.get('type', None)
+            val_error_key = exc_info.get('loc', None)[0]
+            if val_error_type not in ['greater_than_equal',
+                                      'greater_than',
+                                      'less_than',
+                                      'less_than_equal',]:
+                raise exc
+            from module.logger import logger
+
+            try:
+                default_value = self.model_fields[val_error_key].default
+                kwargs[val_error_key] = self.model_fields[val_error_key].default
+                logger.warning(f'Field {val_error_key} is out of range, using default value {default_value}')
+                logger.warning(repr(exc))
+                logger.warning(str(kwargs))
+                super().__init__(*args, **kwargs)
+            except Exception as e:
+                raise
+
+