Skip to content
This repository has been archived by the owner on Jun 13, 2024. It is now read-only.

Commit

Permalink
raw: Handle exception raised by underlying APIs (#180)
Browse files Browse the repository at this point in the history
kind can be provided by number of ways. It is diffcult to
find all cases. So, handle exceptions raised by underlying
APIs and provide good error message to user.

Fixes: ansible/ansible#63652

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
  • Loading branch information
Akasurde committed Aug 11, 2020
1 parent 666b75e commit 4f33ba7
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion plugins/module_utils/raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from ansible.module_utils.basic import missing_required_lib
from ansible_collections.community.kubernetes.plugins.module_utils.common import AUTH_ARG_SPEC, COMMON_ARG_SPEC
from ansible.module_utils.six import string_types
from ansible.module_utils._text import to_native
from ansible_collections.community.kubernetes.plugins.module_utils.common import KubernetesAnsibleModule
from ansible.module_utils.common.dict_transformations import dict_merge

Expand Down Expand Up @@ -175,7 +176,7 @@ def execute_module(self):
continue
kind = definition.get('kind', self.kind)
api_version = definition.get('apiVersion', self.api_version)
if kind.endswith('List'):
if kind and kind.endswith('List'):
resource = self.find_resource(kind, api_version, fail=False)
flattened_definitions.extend(self.flatten_list_kind(resource, definition))
else:
Expand Down Expand Up @@ -269,6 +270,9 @@ def perform_action(self, resource, definition):
except DynamicApiError as exc:
self.fail_json(msg='Failed to retrieve requested object: {0}'.format(exc.body),
error=exc.status, status=exc.status, reason=exc.reason)
except Exception as exc:
self.fail_json(msg='Failed to retrieve requested object: {0}'.format(to_native(exc)),
error='', status='', reason='')

if state == 'absent':
result['method'] = "delete"
Expand Down Expand Up @@ -422,6 +426,12 @@ def patch_resource(self, resource, definition, existing, name, namespace, merge_
msg += "\n" + "\n ".join(self.warnings)
error = dict(msg=msg, error=exc.status, status=exc.status, reason=exc.reason, warnings=self.warnings)
return None, error
except Exception as exc:
msg = "Failed to patch object: {0}".format(exc)
if self.warnings:
msg += "\n" + "\n ".join(self.warnings)
error = dict(msg=msg, error=to_native(exc), status='', reason='', warnings=self.warnings)
return None, error

def create_project_request(self, definition):
definition['kind'] = 'ProjectRequest'
Expand Down

0 comments on commit 4f33ba7

Please sign in to comment.