Skip to content

Commit

Permalink
Handled Exception in Module
Browse files Browse the repository at this point in the history
  • Loading branch information
pyrodie18 committed Oct 22, 2023
1 parent 572dad7 commit 2a7bc16
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions plugins/module_utils/api_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@

from uuid import uuid4

from ansible.module_utils.urls import CertificateError
from ansible.module_utils.connection import ConnectionError
from ansible.module_utils.connection import Connection
from ansible.module_utils._text import to_text
try:
from ansible.module_utils.urls import CertificateError
HAS_CERT_ERROR = True
except ImportError:
HAS_CERT_ERROR = False


class ZabbixApiRequest(object):
Expand All @@ -25,14 +29,22 @@ def __init__(self, module):
self.connection = Connection(self.module._socket_path)

def _httpapi_error_handle(self, payload=None):
try:
code, response = self.connection.send_request(data=payload)
except ConnectionError as e:
self.module.fail_json(msg="connection error occurred: {0}".format(e))
except CertificateError as e:
self.module.fail_json(msg="certificate error occurred: {0}".format(e))
except ValueError as e:
self.module.fail_json(msg="certificate not found: {0}".format(e))
if HAS_CERT_ERROR:
try:
code, response = self.connection.send_request(data=payload)
except ConnectionError as e:
self.module.fail_json(msg="connection error occurred: {0}".format(e))
except CertificateError as e:
self.module.fail_json(msg="certificate error occurred: {0}".format(e))
except ValueError as e:
self.module.fail_json(msg="certificate not found: {0}".format(e))
else:
try:
code, response = self.connection.send_request(data=payload)
except ConnectionError as e:
self.module.fail_json(msg="connection error occurred: {0}".format(e))
except ValueError as e:
self.module.fail_json(msg="certificate not found: {0}".format(e))

if code == 404:
if to_text(u"Object not found") in to_text(response) or to_text(
Expand Down

0 comments on commit 2a7bc16

Please sign in to comment.