Skip to content

Commit 92bff52

Browse files
authored
Merge pull request #18 from T-Systems-MMS/improve_errors
further improve error messages for different errors
2 parents 1862129 + dcdd822 commit 92bff52

1 file changed

Lines changed: 14 additions & 7 deletions

File tree

plugins/module_utils/icinga.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,19 @@ def call_url(self, path, data='', method='GET'):
2929
url = self.module.params.get("url") + "/director" + path
3030
rsp, info = fetch_url(module=self.module, url=url, data=data, headers=headers, method=method,
3131
use_proxy=self.module.params['use_proxy'])
32-
body = ''
32+
content = ''
33+
error = ''
3334
if rsp:
34-
body = json.loads(rsp.read())
35+
content = json.loads(rsp.read())
3536
if info['status'] >= 400:
36-
body = info['body']
37-
return {'code': info['status'], 'data': body, 'msg': info['msg']}
37+
try:
38+
content = json.loads(info["body"])
39+
error = content['error']
40+
except ValueError:
41+
error = info['msg']
42+
if info['status'] < 0:
43+
error = info['msg']
44+
return {'code': info['status'], 'data': content, 'error': error}
3845

3946
def exists(self, find_by='name'):
4047
ret = self.call_url(
@@ -111,7 +118,7 @@ def update(self, state):
111118
changed = True
112119
diff_result.update({'after': 'state: absent\n'})
113120
else:
114-
self.module.fail_json(msg="bad return code while creating: %d. Error message: %s" % (ret['code'], ret['msg']))
121+
self.module.fail_json(msg="bad return code while deleting: %d. Error message: %s" % (ret['code'], ret['error']))
115122
except Exception as e:
116123
self.module.fail_json(msg="exception when deleting: " + str(e))
117124

@@ -128,7 +135,7 @@ def update(self, state):
128135
elif ret['code'] == 304:
129136
changed = False
130137
else:
131-
self.module.fail_json(msg="bad return code while creating: %d. Error message: %s" % (ret['code'], ret['msg']))
138+
self.module.fail_json(msg="bad return code while modifying: %d. Error message: %s" % (ret['code'], ret['error']))
132139

133140
else:
134141
diff_result.update({'before': 'state: absent\n'})
@@ -143,7 +150,7 @@ def update(self, state):
143150
changed = True
144151
diff_result.update({'after': 'state: created\n'})
145152
else:
146-
self.module.fail_json(msg="bad return code while creating: %d. Error message: %s" % (ret['code'], ret['msg']))
153+
self.module.fail_json(msg="bad return code while creating: %d. Error message: %s" % (ret['code'], ret['error']))
147154
except Exception as e:
148155
self.module.fail_json(msg="exception while creating: " + str(e))
149156
return changed, diff_result

0 commit comments

Comments
 (0)