Skip to content

Commit

Permalink
fix: convert parameters eodag names of ValidationError messages to th…
Browse files Browse the repository at this point in the history
…eir STAC names
  • Loading branch information
anesson-cs committed Feb 24, 2025
1 parent 4665cd1 commit d894bc8
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 19 deletions.
3 changes: 2 additions & 1 deletion eodag/api/product/metadata_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -1465,7 +1465,8 @@ def _get_queryables(
# raise an error when a query param not allowed by the provider is found
if not isinstance(md_mapping, list) and raise_mtd_discovery_error:
raise ValidationError(
f"Search parameters which are not queryable are disallowed: {eodag_search_key}"
f"Search parameters which are not queryable are disallowed: {eodag_search_key}",
{eodag_search_key},
)
_, md_value = md_mapping
# query param from defined metadata_mapping
Expand Down
5 changes: 3 additions & 2 deletions eodag/plugins/search/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,8 @@ def build_sort_by(
if eodag_sort_order[:3] != "ASC" and eodag_sort_order[:3] != "DES":
raise ValidationError(
"Sorting order is invalid: it must be set to 'ASC' (ASCENDING) or "
f"'DESC' (DESCENDING), got '{eodag_sort_order}' with '{eodag_sort_param}' instead"
f"'DESC' (DESCENDING), got '{eodag_sort_order}' with '{eodag_sort_param}' instead",
{eodag_sort_param},
)
eodag_sort_order = eodag_sort_order[:3]

Expand All @@ -295,7 +296,7 @@ def build_sort_by(
raise ValidationError(
f"'{eodag_sort_param}' parameter is called several times to sort results with different "
"sorting orders. Please set it to only one ('ASC' (ASCENDING) or 'DESC' (DESCENDING))",
set([eodag_sort_param]),
{eodag_sort_param},
)
provider_sort_by_tuples_used.append(provider_sort_by_tuple)

Expand Down
14 changes: 10 additions & 4 deletions eodag/plugins/search/build_search_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ def discover_queryables(
formated_kwargs[key] = kwargs[key]
else:
raise ValidationError(
f"{key} is not a queryable parameter for {self.provider}"
f"'{key}' is not a queryable parameter for {self.provider}", {key}
)

# we use non empty kwargs as default to integrate user inputs
Expand Down Expand Up @@ -658,7 +658,9 @@ def discover_queryables(
and keyword.replace("ecmwf:", "")
not in set(list(available_values.keys()) + [f["name"] for f in form])
):
raise ValidationError(f"{keyword} is not a queryable parameter")
raise ValidationError(
f"'{keyword}' is not a queryable parameter", {keyword}
)

# generate queryables
if form:
Expand Down Expand Up @@ -745,7 +747,8 @@ def available_values_from_constraints(
# we only compare list of strings.
if isinstance(values, dict):
raise ValidationError(
f"Parameter value as object is not supported: {keyword}={values}"
f"Parameter value as object is not supported: {keyword}={values}",
{keyword},
)
filter_v = values if isinstance(values, (list, tuple)) else [values]

Expand Down Expand Up @@ -810,7 +813,10 @@ def available_values_from_constraints(
raise ValidationError(
f"{keyword}={values} is not available"
f"{all_keywords_str}."
f" Allowed values are {', '.join(allowed_values)}."
f" Allowed values are {', '.join(allowed_values)}.",
set(
[keyword] + [k for k in parsed_keywords if k in input_keywords]
),
)

parsed_keywords.append(keyword)
Expand Down
3 changes: 2 additions & 1 deletion eodag/plugins/search/data_request_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,8 @@ def _create_data_request(
)
raise ValidationError(
f"Search parameters which are not queryable are disallowed for {product_type} with "
f"{self.provider}: please remove '{not_queryable_search_param}' from your search parameters"
f"{self.provider}: please remove '{not_queryable_search_param}' from your search parameters",
{not_queryable_search_param},
)
logger.debug(
f"Sending search job request to {url} with {str(request_body)}"
Expand Down
6 changes: 4 additions & 2 deletions eodag/plugins/search/qssearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,8 @@ def build_query_string(
)
raise ValidationError(
f"Search parameters which are not queryable are disallowed for {product_type} with "
f"{self.provider}: please remove '{not_queryable_search_param}' from your search parameters"
f"{self.provider}: please remove '{not_queryable_search_param}' from your search parameters",
{not_queryable_search_param},
)

# Build the final query string, in one go without quoting it
Expand Down Expand Up @@ -1876,7 +1877,8 @@ def build_query_string(
)
raise ValidationError(
f"Search parameters which are not queryable are disallowed for {product_type} with "
f"{self.provider}: please remove '{not_queryable_search_param}' from your search parameters"
f"{self.provider}: please remove '{not_queryable_search_param}' from your search parameters",
{not_queryable_search_param},
)

# Build the final query string, in one go without quoting it
Expand Down
14 changes: 11 additions & 3 deletions eodag/rest/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
import re
from typing import Union

from fastapi import FastAPI, Request
Expand Down Expand Up @@ -89,10 +90,17 @@ def errors(self) -> list[dict[str, Union[str, int]]]:

if type(exception) is ValidationError:
for error_param in exception.parameters:
stac_param = EODAGSearch.to_stac(error_param)
exception.message = exception.message.replace(
error_param, stac_param
error_param_pattern = rf"[^a-zA-Z_-]?{error_param}[^a-zA-Z_-]?"
error_param_match = re.search(
error_param_pattern, exception.message
)
if error_param_match:
stac_param = EODAGSearch.to_stac(error_param)
exception.message = re.sub(
error_param_pattern,
error_param_match.group(0).replace(error_param, stac_param),
exception.message,
)
error_dict["message"] = exception.message

error_list.append(error_dict)
Expand Down
12 changes: 6 additions & 6 deletions tests/units/test_search_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ def test_plugins_search_querystringsearch_search_peps_ko(self):
"Search parameters which are not queryable are disallowed for "
f"{self.search_criteria_s2_msi_l1c['productType']} with {self.peps_search_plugin.provider}: "
"please remove 'foo' from your search parameters",
str(context.exception),
context.exception.message,
)

# restore the original config
Expand Down Expand Up @@ -451,7 +451,7 @@ def test_plugins_search_querystringsearch_search_peps_ko(self):
"Search parameters which are not queryable are disallowed for "
f"{self.search_criteria_s2_msi_l1c['productType']} with {self.peps_search_plugin.provider}: "
"please remove 'foo' from your search parameters",
str(context.exception),
context.exception.message,
)

# restore the original config
Expand Down Expand Up @@ -2624,8 +2624,8 @@ def test_plugins_search_ecmwfsearch_discover_queryables_ko(self, mock__fetch_dat
with self.assertRaises(ValidationError) as context:
self.search_plugin.discover_queryables(**params)
self.assertEqual(
f"{wrong_queryable} is not a queryable parameter for {self.provider}",
str(context.exception),
f"'{wrong_queryable}' is not a queryable parameter for {self.provider}",
context.exception.message,
)

@mock.patch("eodag.utils.requests.requests.sessions.Session.get", autospec=True)
Expand Down Expand Up @@ -3323,7 +3323,7 @@ def test_plugins_search_postjsonsearchwithstacqueryables_search_wekeomain_ko(sel
"Search parameters which are not queryable are disallowed for "
f"{self.search_criteria_s2_msi_l1c['productType']} with {self.wekeomain_search_plugin.provider}: "
"please remove 'foo' from your search parameters",
str(context.exception),
context.exception.message,
)

# restore the original config
Expand Down Expand Up @@ -3352,7 +3352,7 @@ def test_plugins_search_postjsonsearchwithstacqueryables_search_wekeomain_ko(sel
"Search parameters which are not queryable are disallowed for "
f"{self.search_criteria_s2_msi_l1c['productType']} with {self.wekeomain_search_plugin.provider}: "
"please remove 'foo' from your search parameters",
str(context.exception),
context.exception.message,
)

# restore the original config
Expand Down

0 comments on commit d894bc8

Please sign in to comment.