Skip to content

Commit

Permalink
Raise ValueErrors instead of AssertionErrors on bad params
Browse files Browse the repository at this point in the history
  • Loading branch information
dmannarino committed Jan 13, 2025
1 parent 3bc92f5 commit fbacd70
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 28 deletions.
33 changes: 19 additions & 14 deletions app/models/pydantic/geoencoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 1 addition & 14 deletions app/routes/thematic/geoencoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit fbacd70

Please sign in to comment.