Skip to content

Commit b4e5e4b

Browse files
committed
prettier handling of connection errors
1 parent 5856247 commit b4e5e4b

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

plugins/module_utils/base/api.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,11 @@ def get(self, cnf: dict) -> dict:
5757
)
5858

5959
except HTTPX_EXCEPTIONS as error:
60-
raise api_pretty_exception(
61-
method='GET', error=error,
60+
api_pretty_exception(
61+
m=self.m, method='GET', error=error,
6262
url=f'{self.s.base_url}{call_url}',
6363
)
64+
raise
6465

6566
return response
6667

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

9798
except HTTPX_EXCEPTIONS as error:
98-
raise api_pretty_exception(
99-
method='POST', error=error,
99+
api_pretty_exception(
100+
m=self.m, method='POST', error=error,
100101
url=f'{self.s.base_url}{call_url}',
101102
)
103+
raise
102104

103105
return response
104106

plugins/module_utils/helper/api.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,15 +188,15 @@ def check_response(module: AnsibleModule, cnf: dict, response) -> dict:
188188
return json
189189

190190

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

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

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

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

0 commit comments

Comments
 (0)