-
Notifications
You must be signed in to change notification settings - Fork 1
Open
Description
The error "ERROR: Invalid age format provided" is caused by the code in the get_users() function. Specifically, it occurs when the age parameter is provided in the request, but it cannot be converted to an integer.
To fix this issue, you can modify the code as follows:
File: main.py
def get_users():
age = request.args.get('age')
if age:
try:
age = int(age) # This can raise a ValueError
except ValueError:
logging.error("ERROR: Invalid age format provided.")
return jsonify({"error": "Invalid age format"}), 400
results = []
for entry in data:
if 'age' in entry:
# Avoiding TypeError by ensuring both are integers for comparison
if age and int(entry["age"]) == age:
results.append(entry)
elif not age:
results.append(entry)
return jsonify(results)In this code snippet, the age parameter is first attempted to be converted to an integer using int(age). If a ValueError is raised, it means that the age parameter has an invalid format. In this case, an error message is logged, and a JSON response with an error message and a status code of 400 (Bad Request) is returned.
By adding this error handling code, you can properly handle cases where an invalid age format is provided in the request.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels