From f2e7deb5cba398679360ca5dbf4558eef955ef1f Mon Sep 17 00:00:00 2001 From: Michael Genson <71845777+michael-genson@users.noreply.github.com> Date: Tue, 13 Feb 2024 19:50:38 +0000 Subject: [PATCH] added fault tolerance for malformed recipe json --- mealie/services/migrations/mealie_alpha.py | 29 ++++++++++++++-------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/mealie/services/migrations/mealie_alpha.py b/mealie/services/migrations/mealie_alpha.py index 0886e8d509b3..57fc374ccb6b 100644 --- a/mealie/services/migrations/mealie_alpha.py +++ b/mealie/services/migrations/mealie_alpha.py @@ -5,6 +5,7 @@ import zipfile from pathlib import Path from mealie.schema.recipe.recipe import Recipe +from mealie.schema.reports.reports import ReportEntryCreate from ._migration_base import BaseMigrator from .utils.migration_alias import MigrationAlias @@ -55,20 +56,28 @@ class MealieAlphaMigrator(BaseMigrator): zip_file.extractall(tmpdir) temp_path = Path(tmpdir) - recipe_lookup: dict[str, Path] = {} - recipes_as_dicts = [] - for x in temp_path.rglob("**/recipes/**/[!.]*.json"): - if (y := MigrationReaders.json(x)) is not None: - recipes_as_dicts.append(y) - slug = y["slug"] - recipe_lookup[slug] = x.parent - - recipes = [self._convert_to_new_schema(x) for x in recipes_as_dicts] + recipes: list[Recipe] = [] + for recipe_json_path in temp_path.rglob("**/recipes/**/[!.]*.json"): + try: + if (recipe_as_dict := MigrationReaders.json(recipe_json_path)) is not None: + recipe = self._convert_to_new_schema(recipe_as_dict) + recipes.append(recipe) + slug = recipe_as_dict["slug"] + recipe_lookup[slug] = recipe_json_path.parent + except Exception as e: + self.logger.exception(e) + self.report_entries.append( + ReportEntryCreate( + report_id=self.report_id, + success=False, + message=f"Failed to import {recipe_json_path.name}", + exception=f"{e.__class__.__name__}: {e}", + ) + ) results = self.import_recipes_to_database(recipes) - for slug, recipe_id, status in results: if not status: continue