Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement proper error handling with Flask.request #6

Open
techdev5521 opened this issue Aug 12, 2020 · 0 comments
Open

Implement proper error handling with Flask.request #6

techdev5521 opened this issue Aug 12, 2020 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@techdev5521
Copy link
Owner

Flask's request class stores information about the HTTP request being made to the server per request. request.args returns an ImmutableMultiDict which can be accessed like a normal dict.

Example:

# URL: /api/user?uuid=1
request.args["uuid"] 
>>> 1

When the key trying to be accessed doesn't exist, a BadRequestKeyError. This can be resolved many ways, two of which include:

  • Trying this access in a try/catch loop, catching the BadRequestKeyError
try:
    if request.args["uuid"]:
       # Do some stuff.
except BadRequestKeyError:
    return {"message": "No UUID was provided."}, 422
  • Using the in keyword to test the existence, returning false if the key doesn't exist.
if "uuid" in request.args:
     # Do some stuff.
else:
    return {"message": "No UUID was provided."}, 422

HTTP Code 422

A way forward needs to be agreed upon and the API code needs to be updated for consistency.

@techdev5521 techdev5521 added the bug Something isn't working label Aug 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants