Skip to content

armgit / mwmbl

Public Imported from GitHub
0

An open source, non-profit web search engine

Super Search: skip malformed recipes instead of crashing at import

Daoud Clarke72ab379b1f47
diff --git a/mwmbl/tinysearchengine/super_search_sources/recipe.py b/mwmbl/tinysearchengine/super_search_sources/recipe.py
index b720063..351599e 100644
--- a/mwmbl/tinysearchengine/super_search_sources/recipe.py
+++ b/mwmbl/tinysearchengine/super_search_sources/recipe.py
@@ -66,15 +66,22 @@ def load_recipe(path: Path | str) -> Recipe:
def load_recipes(directory: Path | str = RECIPES_DIR) -> dict[str, Recipe]:
"""Load every ``*.yaml`` recipe in ``directory``, keyed by recipe name.
- A malformed recipe (missing required keys, bad YAML) raises rather than
- being silently skipped, so a broken recipe fails loudly at load time.
+ A malformed recipe (missing required keys, bad YAML) is logged and skipped
+ rather than aborting the whole load. This mirrors the engine's "one broken
+ source can't sink the orchestrator" stance: ``load_recipes`` runs at import
+ time, so raising here would take down the entire app at startup instead of
+ just dropping the one bad recipe.
"""
recipes: dict[str, Recipe] = {}
directory = Path(directory)
if not directory.is_dir():
return recipes
for path in sorted(directory.glob("*.yaml")):
- recipe = load_recipe(path)
+ try:
+ recipe = load_recipe(path)
+ except (KeyError, yaml.YAMLError, OSError) as e:
+ logger.warning("Skipping malformed recipe %s: %s", path.name, e)
+ continue
recipes[recipe.name] = recipe
return recipes
diff --git a/test/test_super_search_recipes.py b/test/test_super_search_recipes.py
index 30868af..9c77a35 100644
--- a/test/test_super_search_recipes.py
+++ b/test/test_super_search_recipes.py
@@ -20,6 +20,27 @@ from mwmbl.tinysearchengine.super_search_sources.recipe import (
RECIPES = load_recipes()
+# A minimal but complete recipe, used to check that valid recipes still load
+# alongside a malformed one.
+GOOD_RECIPE_YAML = """\
+name: good
+domain: example.com
+field: other
+request:
+ url: https://example.com/api
+ params:
+ q: "{query}"
+response:
+ format: json
+ results: results
+ fields:
+ title: title
+ url: url
+smoke:
+ query: x
+ expect_title_contains: x
+"""
+
def _recipe(name: str) -> Recipe:
return RECIPES[name]
@@ -39,10 +60,15 @@ def test_recipes_registered_in_sources():
assert "wiktionary" in SOURCES and "gutenberg" in SOURCES
-def test_load_recipes_raises_on_invalid(tmp_path):
- (tmp_path / "broken.yaml").write_text("name: x\nrequest: {}\n") # no response
- with pytest.raises(KeyError):
- load_recipes(tmp_path)
+def test_load_recipes_skips_malformed(tmp_path, caplog):
+ """A malformed recipe is logged and skipped, not raised — so one bad file
+ can't crash the whole app at import time. Valid recipes still load."""
+ (tmp_path / "broken.yaml").write_text("name: x\nrequest: {}\n") # missing keys
+ (tmp_path / "good.yaml").write_text(GOOD_RECIPE_YAML)
+ with caplog.at_level("WARNING"):
+ recipes = load_recipes(tmp_path)
+ assert set(recipes) == {"good"}
+ assert "broken.yaml" in caplog.text
@pytest.mark.parametrize("recipe", RECIPES.values(), ids=lambda r: r.name)