Skip to content

Commit

Permalink
Don't throw an exception if API's JSON response lacks an expected key
Browse files Browse the repository at this point in the history
Instead of throwing an exception, be more defensive in the code: if
the API's JSON response lacks a key and we correspondingly set a
variable to `None`, then don't call `get()` on the variable, but test
the variable first.

    AttributeError: 'NoneType' object has no attribute 'get'

Fixes #272.
  • Loading branch information
coreywright committed Jun 26, 2022
1 parent b551430 commit 5cb930f
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
2 changes: 2 additions & 0 deletions changelogs/fragments/273-Dont_call_get_on_None.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- digital_ocean_droplet - if the JSON response lacks a key and the associated variable is set to ``None``, then don't treat that variable like a ``dict`` and call ``get()`` on it without first testing it (https://github.com/ansible-collections/community.digitalocean/issues/272).
2 changes: 1 addition & 1 deletion plugins/modules/digital_ocean_droplet.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ def wait_status(self, droplet_id, desired_statuses):
status_code = response.status_code
message = json_data.get("message", "no error message")
droplet = json_data.get("droplet", None)
droplet_status = droplet.get("status", None)
droplet_status = droplet.get("status", None) if droplet else None

if droplet is None or droplet_status is None:
self.module.fail_json(
Expand Down

0 comments on commit 5cb930f

Please sign in to comment.