From fbacd70ef44f66947f78a78c3d177ebd39ef8970 Mon Sep 17 00:00:00 2001 From: Daniel Mannarino Date: Mon, 13 Jan 2025 11:07:06 -0500 Subject: [PATCH] Raise ValueErrors instead of AssertionErrors on bad params --- app/models/pydantic/geoencoder.py | 33 ++++++++++++++++++------------- app/routes/thematic/geoencoder.py | 15 +------------- 2 files changed, 20 insertions(+), 28 deletions(-) diff --git a/app/models/pydantic/geoencoder.py b/app/models/pydantic/geoencoder.py index 6c4e687c..343e5916 100644 --- a/app/models/pydantic/geoencoder.py +++ b/app/models/pydantic/geoencoder.py @@ -72,25 +72,30 @@ class GeoencoderQueryParams(StrictBaseModel): @root_validator() def validate_params(cls, values): source = values.get("admin_source") - assert ( - source is not None - ), "Must provide admin_source or leave unset for default of GADM" + if source is None: + raise ValueError( + "Must provide admin_source or leave unset for default of GADM" + ) + version = values.get("admin_version") - assert version is not None, "Must provide admin_version" + if version is None: + raise ValueError("Must provide an admin_version") - sources_in_this_env = per_env_admin_boundary_versions.get(ENV) + sources_in_this_env = per_env_admin_boundary_versions[ENV] versions_of_source_in_this_env = sources_in_this_env.get(source) - assert versions_of_source_in_this_env is not None, ( - f"Invalid administrative boundary source {source}. Valid " - f"sources in this environment are {[v for v in sources_in_this_env.keys()]}" - ) + if versions_of_source_in_this_env is None: + ValueError( + f"Invalid administrative boundary source {source}. Valid " + f"sources in this environment are {[v for v in sources_in_this_env.keys()]}" + ) deployed_version_in_data_api = versions_of_source_in_this_env.get(version) - assert deployed_version_in_data_api is not None, ( - f"Invalid version {version} for administrative boundary source " - f"{source}. Valid versions for this source in this environment are " - f"{[v.value for v in versions_of_source_in_this_env.keys()]}" - ) + if deployed_version_in_data_api is None: + ValueError( + f"Invalid version {version} for administrative boundary source " + f"{source}. Valid versions for this source in this environment are " + f"{[v.value for v in versions_of_source_in_this_env.keys()]}" + ) return values diff --git a/app/routes/thematic/geoencoder.py b/app/routes/thematic/geoencoder.py index c6585c78..f213a106 100644 --- a/app/routes/thematic/geoencoder.py +++ b/app/routes/thematic/geoencoder.py @@ -161,19 +161,6 @@ def _admin_boundary_lookup_sql( def lookup_admin_source_version(source, version) -> str: - sources_in_this_env: Dict = per_env_admin_boundary_versions.get(ENV) - - versions_of_source_in_this_env: Dict = sources_in_this_env.get(source) - assert versions_of_source_in_this_env is not None, ( - f"Invalid administrative boundary source {source}. Valid " - f"sources in this environment are {[v for v in sources_in_this_env.keys()]}" - ) - - deployed_version_in_data_api = versions_of_source_in_this_env.get(version) - assert deployed_version_in_data_api is not None, ( - f"Invalid version {version} for administrative boundary source " - f"{source}. Valid versions for this source in this environment are " - f"{[v.value for v in versions_of_source_in_this_env.keys()]}" - ) + deployed_version_in_data_api = per_env_admin_boundary_versions[ENV][source][version] return deployed_version_in_data_api