diff --git a/project_types/validate/api_calls.py b/project_types/validate/api_calls.py index 52ff2019..e709e0d3 100644 --- a/project_types/validate/api_calls.py +++ b/project_types/validate/api_calls.py @@ -185,7 +185,11 @@ def get_object_count_from_ohsome(area: str, ohsome_filter: PydanticLongText) -> logger.info("Filter: %s", ohsome_filter) # fixme(frozenhelium): use httpx for proper timeout - response = requests.post(url, data=data, timeout=100) + try: + response = requests.post(url, data=data, timeout=1) + except requests.exceptions.Timeout as e: + logger.warning("ohsome element count request timed out") + raise ValidateApiCallError("OHSOME request timed out.") from e if response.status_code != 200: logger.warning( "ohsome element count request failed: check for errors in filter or geometries", @@ -219,7 +223,11 @@ def ohsome(request: dict[str, Any], area: str, properties: str | None = None) -> logger.info("Target: %s", url) logger.info("Filter: %s", request["filter"]) # FIXME(tnagorra): Need to check what the timeout should be - response = requests.post(url, data=data, timeout=100) + try: + response = requests.post(url, data=data, timeout=100) + except requests.exceptions.Timeout as e: + logger.warning("ohsome request timed out") + raise ValidateApiCallError("OHSOME request timed out.") from e if response.status_code != 200: logger.warning( "ohsome request failed: check for errors in filter or geometries", diff --git a/project_types/validate/project.py b/project_types/validate/project.py index bae94c49..c997e6ed 100644 --- a/project_types/validate/project.py +++ b/project_types/validate/project.py @@ -159,7 +159,12 @@ def test_geojson_url_project(url: str): # FIXME(frozenhelium): use predefined timeout duration # FIXME(tnagorra): handle timeout error - response = requests.get(url, timeout=500) + try: + response = requests.get(url, timeout=500) + except requests.exceptions.Timeout as e: + raise base_project.ValidationException( + f"Failed to fetch object geojson from {url}: request timed out", + ) from e if response.status_code != 200: raise base_project.ValidationException( f"Failed to fetch object geojson from {url}", @@ -207,6 +212,8 @@ def test_ohsome_objects_from_aoi_asset( feature_collection.model_dump_json(), ohsome_filter, ) + except ValidateApiCallError as e: + raise base_project.ValidationException(str(e) or "Failed to get object_count from ohsome") from e except Exception as e: raise base_project.ValidationException("Failed to get object_count from ohsome") from e @@ -223,7 +230,12 @@ def test_tasking_manager_project( hot_tm_url = f"{Config.HOT_TASKING_MANAGER_PROJECT_API_LINK}projects/{hot_tm_id}/queries/aoi/?as_file=false" logger.info("Fetching AOI geojson on HOT from %s", hot_tm_url) - aoi_result = requests.get(hot_tm_url, timeout=500) + try: + aoi_result = requests.get(hot_tm_url, timeout=500) + except requests.exceptions.Timeout as e: + raise base_project.ValidationException( + f"Failed to fetch AOI GeoJSON from HOT Tasking Manager for tm_id {hot_tm_id}: request timed out", + ) from e if aoi_result.status_code != 200: raise base_project.ValidationException( f"Failed to fetch AOI GeoJSON from HOT Tasking Manager for tm_id {hot_tm_id}", @@ -269,6 +281,8 @@ def test_tasking_manager_project( feature_collection.model_dump_json(), ohsome_filter, ) + except ValidateApiCallError as e: + raise base_project.ValidationException(str(e) or "Failed to get object_count from ohsome") from e except Exception as e: raise base_project.ValidationException("Failed to get object_count from ohsome") from e @@ -313,7 +327,9 @@ def _get_object_geometry_from_ohsome(self, geojson: dict[typing.Any, typing.Any] ) except ValidateApiCallError as e: # NOTE: Handles calls from OHSOME, OSMCHA and OSM - raise base_project.ValidationException("Failed to fetch data from OHSOME/OSMCHA/OSM") from e + raise base_project.ValidationException( + str(e) or "Failed to fetch data from OHSOME/OSMCHA/OSM", + ) from e except requests.JSONDecodeError as e: # NOTE: Handles calls from OHSOME and OSMCHA # OSM responds in XML format