Skip to content

Commit

Permalink
prettier handling of connection errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ansibleguy committed Jul 4, 2024
1 parent 5856247 commit b4e5e4b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
10 changes: 6 additions & 4 deletions plugins/module_utils/base/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@ def get(self, cnf: dict) -> dict:
)

except HTTPX_EXCEPTIONS as error:
raise api_pretty_exception(
method='GET', error=error,
api_pretty_exception(
m=self.m, method='GET', error=error,
url=f'{self.s.base_url}{call_url}',
)
raise

return response

Expand Down Expand Up @@ -95,10 +96,11 @@ def post(self, cnf: dict, headers: dict = None) -> dict:
)

except HTTPX_EXCEPTIONS as error:
raise api_pretty_exception(
method='POST', error=error,
api_pretty_exception(
m=self.m, method='POST', error=error,
url=f'{self.s.base_url}{call_url}',
)
raise

return response

Expand Down
8 changes: 4 additions & 4 deletions plugins/module_utils/helper/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,15 +188,15 @@ def check_response(module: AnsibleModule, cnf: dict, response) -> dict:
return json


def api_pretty_exception(method: str, url: str, error) -> ConnectionError:
def api_pretty_exception(m: AnsibleModule, method: str, url: str, error):
call = f'{method} => {url}'
msg = f"Unable to connect '{call}'!"
msg = f"Unable to connect '{call}'"

if str(error).find('timed out') != -1:
msg = f"Got timeout calling '{call}'!"
msg = f"Got timeout calling '{call}'"

if str(error).find('CERTIFICATE_VERIFY_FAILED') != -1 or str(error).find('certificate verify failed') != -1:
msg = f"SSL verification failed '{url}'! Make sure to follow the the documentation: "\
"https://opnsense.ansibleguy.net/en/latest/usage/2_basic.html#ssl-certificate"

return ConnectionError(msg)
m.fail_json(f"{msg} ({error})")

0 comments on commit b4e5e4b

Please sign in to comment.