Skip to content

Commit

Permalink
Respond with clear message
Browse files Browse the repository at this point in the history
  • Loading branch information
NicholasTurner23 committed Dec 4, 2024
1 parent ce62981 commit 931d18d
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/analytics/api/views/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ def post(self):
json_data
)
if error_message:
return error_message, AirQoRequests.Status.HTTP_400_BAD_REQUEST
return (
AirQoRequests.create_response(error_message, success=False),
AirQoRequests.Status.HTTP_400_BAD_REQUEST,
)
except Exception as e:
logger.exception(f"An error has occured; {e}")

Expand Down Expand Up @@ -209,8 +212,9 @@ def _get_validated_filter(self, json_data):
Raises:
ValueError: If more than one or none of the filters are provided.
"""
error_message: str = ""
filter_type: str = None
validated_data: List[str] = None
error_message: str = ""

# TODO Lias with device registry to cleanup this makeshift implementation
devices = ["devices", "device_ids", "device_names"]
Expand All @@ -227,9 +231,9 @@ def _get_validated_filter(self, json_data):
]
provided_filters = [key for key in valid_filters if json_data.get(key)]
if len(provided_filters) != 1:
raise ValueError(
"Specify exactly one of 'airqlouds', 'sites', 'device_names', or 'devices' in the request body."
)
error_message = "Specify exactly one of 'airqlouds', 'sites', 'device_names', or 'devices' in the request body."
return filter_type, validated_data, error_message

filter_type = provided_filters[0]
filter_value = json_data.get(filter_type)

Expand All @@ -246,7 +250,9 @@ def _get_validated_filter(self, json_data):
"sites" if filter_type in sites else "devices", []
)
else:
error_message = validated_value.get("message", "Validation failed")
error_message = validated_value.get(
"message", "Data filter validation failed"
)

return filter_type, validated_data, error_message

Expand Down

0 comments on commit 931d18d

Please sign in to comment.