Skip to content

Commit 3dab674

Browse files
authored
fix: don't crash in fail_taxonomy if PO server is not responding (#1856)
Product Opener is currently closing connection when doing a HEAD https://static.openfoodfacts.org/data/taxonomies/categories.full.json. Here, we're trying to fallback on cached version first, then on offline taxonomy.
1 parent f27c139 commit 3dab674

File tree

1 file changed

+38
-6
lines changed

1 file changed

+38
-6
lines changed

robotoff/taxonomy.py

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,50 @@ def get_taxonomy(taxonomy_type: TaxonomyType | str, offline: bool = False) -> Ta
5858
defaults to False. It's not available for all taxonomy types.
5959
:return: the Taxonomy
6060
"""
61+
taxonomy_offline_path = str(settings.TAXONOMY_PATHS[taxonomy_type])
62+
cache_dir = settings.DATA_DIR / "taxonomies"
6163
if offline:
62-
return Taxonomy.from_path(str(settings.TAXONOMY_PATHS[taxonomy_type]))
64+
return Taxonomy.from_path(taxonomy_offline_path)
6365

6466
taxonomy_type_enum = (
6567
TaxonomyType[taxonomy_type] if isinstance(taxonomy_type, str) else taxonomy_type
6668
)
67-
return _get_taxonomy(
68-
taxonomy_type_enum,
69-
force_download=False,
70-
download_newer=True,
71-
cache_dir=settings.DATA_DIR / "taxonomies",
69+
70+
try:
71+
return _get_taxonomy(
72+
taxonomy_type_enum,
73+
force_download=False,
74+
download_newer=True,
75+
cache_dir=cache_dir,
76+
)
77+
except Exception as e:
78+
logger.error(
79+
"Error while fetching taxonomy %s: %s.",
80+
taxonomy_type,
81+
e,
82+
)
83+
84+
try:
85+
logger.info("Trying to load taxonomy %s from local cache...", taxonomy_type)
86+
return _get_taxonomy(
87+
taxonomy_type_enum,
88+
force_download=False,
89+
download_newer=False,
90+
cache_dir=cache_dir,
91+
)
92+
except Exception as e:
93+
logger.info(
94+
"No cached version of taxonomy %s found or error while loading it: %s. ",
95+
taxonomy_type,
96+
e,
97+
)
98+
99+
logger.info(
100+
"Loading taxonomy %s from local static file %s...",
101+
taxonomy_type,
102+
taxonomy_offline_path,
72103
)
104+
return Taxonomy.from_path(taxonomy_offline_path)
73105

74106

75107
def is_prefixed_value(value: str) -> bool:

0 commit comments

Comments
 (0)