fix error handling

This commit is contained in:
psy 2021-01-04 18:24:01 +01:00
parent b99410f135
commit 05063e0077
No known key found for this signature in database
GPG key ID: DFF5B17AC3A8ECF5

View file

@ -24,6 +24,9 @@ api = RocketChat(botUser, botPass, server_url=botChatURL, session=api_session)
def postAlertmanager(chatName):
try:
content = json.loads(request.get_data())
res = True
for alert in content['alerts']:
message = ""
if alert['status'] == "firing":
@ -56,12 +59,15 @@ def postAlertmanager(chatName):
for l in alert['labels']:
labels += l + " : "+alert['labels'][l] + "\n"
message += labels + "```\n"
if api.chat_post_message(message, channel=chatName, alias='Alertmanager').ok:
res &= api.chat_post_message(message, channel=chatName, alias='Alertmanager').ok
if res:
return "Alert OK", 200
else:
return "Alert fail", 200
except Exception as error:
bot.sendMessage(chat_id=chatID, text="Error to read json: "+str(error))
api.chat_post_message("Error to read json: "+str(error), channel=chatName, alias='Alertmanager')
app.logger.info("\t%s",error)
return "Alert fail", 200