Skip to content

Commit f1957c1

Browse files
committed
feat: 增强配置加载错误处理,添加 YAML 和 Pydantic 验证错误日志
1 parent 1da8786 commit f1957c1

1 file changed

Lines changed: 8 additions & 9 deletions

File tree

app/models/config.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from functools import lru_cache
2727

2828
import yaml
29-
from pydantic import model_validator, BaseModel
29+
from pydantic import model_validator, BaseModel, ValidationError
3030

3131
logger = logging.getLogger(__name__)
3232

@@ -88,14 +88,13 @@ def from_yaml(cls, yaml_file: str = "config.yaml"):
8888
# 使用 model_validate 创建实例
8989
try:
9090
return cls.model_validate(config_data)
91-
except Exception as e: # pylint: disable=broad-except
92-
logger.warning("配置验证失败: %s, Exception: %s", str(e), type(e).__name__)
93-
logger.warning("使用默认配置")
94-
95-
# 更新基本配置
96-
for key, value in config_data.items():
97-
if key != "WEBHOOK" and hasattr(config, key):
98-
setattr(config, key, value)
91+
except yaml.YAMLError as e:
92+
logger.error("YAML配置加载失败: %s", e)
93+
raise
94+
except ValidationError as e:
95+
logger.error("Pydantic配置验证失败: %s", e)
96+
raise
97+
9998
else:
10099
logger.warning("警告:配置文件 %s 不存在,使用默认配置", config_path)
101100

0 commit comments

Comments
 (0)