From 77f6f5031587a8fc5c220cd3e4b58ea3ef30fe0e Mon Sep 17 00:00:00 2001 From: Yinan Zhou Date: Fri, 23 Jan 2026 16:35:30 -0500 Subject: [PATCH] fix: add http error handling --- .../management/commands/import_languages.py | 33 +++++++++++-------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/web-app/django/VIM/apps/instruments/management/commands/import_languages.py b/web-app/django/VIM/apps/instruments/management/commands/import_languages.py index 19219a0b..982022b8 100644 --- a/web-app/django/VIM/apps/instruments/management/commands/import_languages.py +++ b/web-app/django/VIM/apps/instruments/management/commands/import_languages.py @@ -141,20 +141,25 @@ def get_language_directions_from_sparql(url: str): } GROUP BY ?code """ - response = requests.get( - url, params={"query": query, "format": "json"}, headers=HEADERS, timeout=200 - ) - data = response.json() - - directions = {} - for item in data.get("results", {}).get("bindings", []): - code = item["code"]["value"].lower() - direction_label = item["direction"]["value"] - if "right-to-left" in direction_label: - directions[code] = "rtl" - else: - directions[code] = "ltr" - return directions + try: + response = requests.get( + url, params={"query": query, "format": "json"}, headers=HEADERS, timeout=200 + ) + response.raise_for_status() + data = response.json() + + directions = {} + for item in data.get("results", {}).get("bindings", []): + code = item["code"]["value"].lower() + direction_label = item["direction"]["value"] + if "right-to-left" in direction_label: + directions[code] = "rtl" + else: + directions[code] = "ltr" + return directions + except requests.RequestException as e: + print(f"Error: Failed to fetch language direction data. {e}") + return {} class Command(BaseCommand):