mirror of
https://github.com/HKUDS/nanobot.git
synced 2026-06-15 15:24:06 +00:00
Fail fast on invalid config files
This commit is contained in:
parent
5d7f2e60c2
commit
aee656eb9f
@ -7,7 +7,6 @@ from pathlib import Path
|
|||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
import pydantic
|
import pydantic
|
||||||
from loguru import logger
|
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
from nanobot.config.schema import Config, _resolve_tool_config_refs
|
from nanobot.config.schema import Config, _resolve_tool_config_refs
|
||||||
@ -55,8 +54,7 @@ def load_config(config_path: Path | None = None) -> Config:
|
|||||||
data = _migrate_config(data)
|
data = _migrate_config(data)
|
||||||
config = Config.model_validate(data)
|
config = Config.model_validate(data)
|
||||||
except (json.JSONDecodeError, ValueError, pydantic.ValidationError) as e:
|
except (json.JSONDecodeError, ValueError, pydantic.ValidationError) as e:
|
||||||
logger.warning("Failed to load config from {}: {}", path, e)
|
raise ValueError(f"Failed to load config from {path}: {e}") from e
|
||||||
logger.warning("Using default configuration.")
|
|
||||||
|
|
||||||
_apply_ssrf_whitelist(config)
|
_apply_ssrf_whitelist(config)
|
||||||
return config
|
return config
|
||||||
|
|||||||
30
tests/config/test_config_load_errors.py
Normal file
30
tests/config/test_config_load_errors.py
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
import json
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from nanobot.config.loader import load_config
|
||||||
|
|
||||||
|
|
||||||
|
def test_load_config_missing_file_uses_defaults(tmp_path) -> None:
|
||||||
|
config = load_config(tmp_path / "missing.json")
|
||||||
|
|
||||||
|
assert config.agents.defaults.model
|
||||||
|
|
||||||
|
|
||||||
|
def test_load_config_invalid_json_fails_fast(tmp_path) -> None:
|
||||||
|
config_path = tmp_path / "config.json"
|
||||||
|
config_path.write_text("{broken json", encoding="utf-8")
|
||||||
|
|
||||||
|
with pytest.raises(ValueError, match="Failed to load config"):
|
||||||
|
load_config(config_path)
|
||||||
|
|
||||||
|
|
||||||
|
def test_load_config_invalid_schema_fails_fast(tmp_path) -> None:
|
||||||
|
config_path = tmp_path / "config.json"
|
||||||
|
config_path.write_text(
|
||||||
|
json.dumps({"tools": {"exec": {"timeout": -1}}}),
|
||||||
|
encoding="utf-8",
|
||||||
|
)
|
||||||
|
|
||||||
|
with pytest.raises(ValueError, match="Failed to load config"):
|
||||||
|
load_config(config_path)
|
||||||
Loading…
x
Reference in New Issue
Block a user