From d6b917411223d23ee0ae40643ceca4eb4f720b35 Mon Sep 17 00:00:00 2001 From: JHCD Date: Tue, 23 Jun 2015 22:27:48 +0200 Subject: [PATCH] change exception-handling - only error-msg instead of logging.exception - second logging.debug with exec-trace --- includes/alarmHandler.py | 5 +--- includes/decoders/fms.py | 11 +++++--- includes/decoders/poc.py | 13 ++++++---- includes/decoders/zvei.py | 11 +++++--- plugins/BosMon/BosMon.py | 16 +++++++----- plugins/MySQL/MySQL.py | 32 +++++++++++++++-------- plugins/eMail/eMail.py | 38 ++++++++++++++++++++-------- plugins/firEmergency/firEmergency.py | 34 +++++++++++++++++-------- plugins/httpRequest/httpRequest.py | 25 ++++++++++++------ plugins/jsonSocket/jsonSocket.py | 8 +++++- 10 files changed, 132 insertions(+), 61 deletions(-) diff --git a/includes/alarmHandler.py b/includes/alarmHandler.py index fa4ab19..adb8e23 100644 --- a/includes/alarmHandler.py +++ b/includes/alarmHandler.py @@ -48,7 +48,6 @@ def processAlarm(typ,freq,data): logging.debug("return from: %s", pluginName) except: # call next plugin, if one has thrown an exception - logging.debug("return from: %s", pluginName, exc_info=True) pass else: # RegEX filter off - call plugin directly logging.debug("call Plugin: %s", pluginName) @@ -57,9 +56,7 @@ def processAlarm(typ,freq,data): logging.debug("return from: %s", pluginName) except: # call next plugin, if one has thrown an exception - logging.debug("return from: %s", pluginName, exc_info=True) pass logging.debug("[END ALARM]") except: - logging.error("Error in Alarm processing") - logging.debug("Error in Alarm processing", exc_info=True) \ No newline at end of file + logging.exception("Error in alarm processing") \ No newline at end of file diff --git a/includes/decoders/fms.py b/includes/decoders/fms.py index 3ef24f2..02da649 100644 --- a/includes/decoders/fms.py +++ b/includes/decoders/fms.py @@ -62,9 +62,14 @@ def decode(freq, decoded): from includes import descriptionList data["description"] = descriptionList.getDescription("FMS", fms_id[0:8]) # processing the alarm - from includes import alarmHandler - alarmHandler.processAlarm("FMS",freq,data) - + try: + from includes import alarmHandler + alarmHandler.processAlarm("POC",freq,data) + except: + logging.error("processing alarm failed") + logging.debug("processing alarm failed", exc_info=True) + pass + # in every time save old data for double alarm globals.fms_id_old = fms_id #save last id globals.fms_time_old = timestamp #save last time else: diff --git a/includes/decoders/poc.py b/includes/decoders/poc.py index ce4d570..ceba913 100644 --- a/includes/decoders/poc.py +++ b/includes/decoders/poc.py @@ -105,8 +105,6 @@ def decode(freq, decoded): if re.search("[0-9]{7}", poc_id): #if POC is valid if isAllowed(poc_id): # check for double alarm - logging.debug(" - old id: %s ", globals.poc_id_old) - logging.debug(" - old time: %s ", globals.poc_time_old) if poc_id == globals.poc_id_old and timestamp < globals.poc_time_old + globals.config.getint("POC", "double_ignore_time"): logging.info("POCSAG%s double alarm: %s within %s second(s)", bitrate, globals.poc_id_old, timestamp-globals.poc_time_old) # in case of double alarm, poc_double_ignore_time set new @@ -121,9 +119,14 @@ def decode(freq, decoded): from includes import descriptionList data["description"] = descriptionList.getDescription("POC", poc_id) # processing the alarm - from includes import alarmHandler - alarmHandler.processAlarm("POC",freq,data) - + try: + from includes import alarmHandler + alarmHandler.processAlarm("POC",freq,data) + except: + logging.error("processing alarm failed") + logging.debug("processing alarm failed", exc_info=True) + pass + # in every time save old data for double alarm globals.poc_id_old = poc_id #save last id globals.poc_time_old = timestamp #save last time else: diff --git a/includes/decoders/zvei.py b/includes/decoders/zvei.py index 789d5da..e941e75 100644 --- a/includes/decoders/zvei.py +++ b/includes/decoders/zvei.py @@ -74,9 +74,14 @@ def decode(freq, decoded): from includes import descriptionList data["description"] = descriptionList.getDescription("ZVEI", zvei_id) # processing the alarm - from includes import alarmHandler - alarmHandler.processAlarm("ZVEI",freq,data) - + try: + from includes import alarmHandler + alarmHandler.processAlarm("POC",freq,data) + except: + logging.error("processing alarm failed") + logging.debug("processing alarm failed", exc_info=True) + pass + # in every time save old data for double alarm globals.zvei_id_old = zvei_id # save last id globals.zvei_time_old = timestamp # save last time else: diff --git a/plugins/BosMon/BosMon.py b/plugins/BosMon/BosMon.py index cf5da65..9b9aed6 100644 --- a/plugins/BosMon/BosMon.py +++ b/plugins/BosMon/BosMon.py @@ -35,6 +35,7 @@ def bosMonRequest(httprequest, params, headers): @param headers: The headers argument should be a mapping of extra HTTP headers to send with the request. @return: nothing + @exception: Exception if HTTP-Request failed """ try: # @@ -44,7 +45,7 @@ def bosMonRequest(httprequest, params, headers): except: logging.error("request to BosMon failed") logging.debug("request to BosMon failed", exc_info=True) - raise Exception("request to BosMon failed") + raise else: # # check HTTP-Response @@ -94,7 +95,7 @@ def run(typ,freq,data): return try: - # + # # Initialize header an connect to BosMon-Server # headers = {} @@ -116,7 +117,7 @@ def run(typ,freq,data): return else: - # + # # Format given data-structure to compatible BosMon string # if typ == "FMS": @@ -179,9 +180,12 @@ def run(typ,freq,data): finally: logging.debug("close BosMon-Connection") - httprequest.close() - - except: + try: + httprequest.close() + except: + pass + + except: # something very mysterious logging.error("unknown error") logging.debug("unknown error", exc_info=True) \ No newline at end of file diff --git a/plugins/MySQL/MySQL.py b/plugins/MySQL/MySQL.py index 887d314..2ef31a6 100644 --- a/plugins/MySQL/MySQL.py +++ b/plugins/MySQL/MySQL.py @@ -43,19 +43,20 @@ def run(typ,freq,data): @requires: Created Database/Tables, see boswatch.sql @return: nothing - @exception: Exception if ConfigParser failed - @exception: Exception if connect to MySQL failed - @exception: Exception if executing the sql-statement is failed """ - try: + # #ConfigParser + # logging.debug("reading config file") try: for key,val in globals.config.items("MySQL"): logging.debug(" - %s = %s", key, val) except: - logging.exception("cannot read config file") + logging.error("cannot read config file") + logging.debug("cannot read config file", exc_info=True) + # Without config, plugin couldn't work + return try: # @@ -65,7 +66,11 @@ def run(typ,freq,data): connection = mysql.connector.connect(host = globals.config.get("MySQL","dbserver"), user = globals.config.get("MySQL","dbuser"), passwd = globals.config.get("MySQL","dbpassword"), db = globals.config.get("MySQL","database")) cursor = connection.cursor() except: - logging.exception("cannot connect to MySQL") + logging.error("cannot connect to MySQL") + logging.debug("cannot connect to MySQL", exc_info=True) + # Without connection, plugin couldn't work + return + else: try: # @@ -89,11 +94,18 @@ def run(typ,freq,data): else: logging.warning("Invalid Typ: %s", typ) except: - logging.exception("cannot Insert %s", typ) + logging.error("cannot Insert %s", typ) + logging.debug("cannot Insert %s", typ, exc_info=True) + return finally: logging.debug("close MySQL") - cursor.close() - connection.close() #Close connection in every case + try: + cursor.close() + connection.close() #Close connection in every case + except: + pass + except: - logging.exception("unknown error") \ No newline at end of file + logging.error("unknown error") + logging.debug("unknown error", exc_info=True) \ No newline at end of file diff --git a/plugins/eMail/eMail.py b/plugins/eMail/eMail.py index 24477a5..f879431 100644 --- a/plugins/eMail/eMail.py +++ b/plugins/eMail/eMail.py @@ -54,7 +54,9 @@ def doSendmail(server, subject, mailtext): msg['Priority'] = globals.config.get("eMail", "priority") server.sendmail(globals.config.get("eMail", "from"), globals.config.get("eMail", "to"), msg.as_string()) except: - logging.exception("send eMail failed") + logging.error("send eMail failed") + logging.debug("send eMail failed", exc_info=True) + raise ## @@ -80,9 +82,6 @@ def run(typ,freq,data): @requires: eMail-Configuration has to be set in the config.ini @return: nothing - @exception: Exception if ConfigParser failed - @exception: Exception if connect to SMTP-Server failed - @exception: Exception if sending the eMail failed """ try: # @@ -94,7 +93,10 @@ def run(typ,freq,data): logging.debug(" - %s = %s", key, val) except: - logging.exception("cannot read config file") + logging.error("cannot read config file") + logging.debug("cannot read config file", exc_info=True) + # Without config, plugin couldn't work + return try: # @@ -113,7 +115,10 @@ def run(typ,freq,data): server.login(globals.config.get("eMail", "user"), globals.config.get("eMail", "password")) except: - logging.exception("cannot connect to eMail") + logging.error("cannot connect to eMail") + logging.debug("cannot connect to eMail", exc_info=True) + # Without connection, plugin couldn't work + return else: @@ -137,7 +142,9 @@ def run(typ,freq,data): # send eMail doSendmail(server, subject, mailtext) except: - logging.exception("FMS to eMail failed") + logging.error("%s to eMail failed", typ) + logging.debug("%s to eMail failed", typ, exc_info=True) + return elif typ == "ZVEI": logging.debug("Start ZVEI to eMail") @@ -155,7 +162,9 @@ def run(typ,freq,data): # send eMail doSendmail(server, subject, mailtext) except: - logging.exception("ZVEI to eMail failed") + logging.error("%s to eMail failed", typ) + logging.debug("%s to eMail failed", typ, exc_info=True) + return elif typ == "POC": logging.debug("Start POC to eMail") @@ -177,14 +186,21 @@ def run(typ,freq,data): # send eMail doSendmail(server, subject, mailtext) except: - logging.exception("POC to eMail failed") + logging.error("%s to eMail failed", typ) + logging.debug("%s to eMail failed", typ, exc_info=True) + return else: logging.warning("Invalid Typ: %s", typ) finally: logging.debug("close eMail-Connection") - server.quit() + try: + server.quit() + except: + pass except: - logging.exception("") \ No newline at end of file + # something very mysterious + logging.error("unknown error") + logging.debug("unknown error", exc_info=True) \ No newline at end of file diff --git a/plugins/firEmergency/firEmergency.py b/plugins/firEmergency/firEmergency.py index 3b2a4b2..83cbcb6 100644 --- a/plugins/firEmergency/firEmergency.py +++ b/plugins/firEmergency/firEmergency.py @@ -36,9 +36,6 @@ def run(typ,freq,data): @requires: firEmergency-Configuration has to be set in the config.ini @return: nothing - @exception: Exception if ConfigParser failed - @exception: Exception ifconnect to firEmergency failed - @exception: Exception if sending the data failed """ try: # @@ -49,7 +46,10 @@ def run(typ,freq,data): for key,val in globals.config.items("firEmergency"): logging.debug(" - %s = %s", key, val) except: - logging.exception("cannot read config file") + logging.error("cannot read config file") + logging.debug("cannot read config file", exc_info=True) + # Without config, plugin couldn't work + return try: # @@ -58,7 +58,11 @@ def run(typ,freq,data): firSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) firSocket.connect((globals.config.get("firEmergency", "firserver"), globals.config.getint("firEmergency", "firport"))) except: - logging.exception("cannot connect to firEmergency") + logging.error("cannot connect to firEmergency") + logging.debug("cannot connect to firEmergency", exc_info=True) + # Without connection, plugin couldn't work + return + else: # # Format given data-structure to xml-string for firEmergency @@ -72,22 +76,32 @@ def run(typ,freq,data): firXML = "\n
"+data["zvei"]+"
\n"+data["zvei"]+" alarmiert.\n
\n" firSocket.send(firXML) except: - logging.exception("ZVEI to firEmergency failed") - + logging.error("%s to firEmergency failed", typ) + logging.debug("%s to firEmergency failed", typ, exc_info=True) + # Without connection, plugin couldn't work + return + elif typ == "POC": logging.debug("POC to firEmergency") try: firXML = "\n
"+data["ric"]+"
\n"+data["msg"]+"\n
\n" firSocket.send(firXML) except: - logging.exception("POC to firEmergency failed") + logging.error("%s to firEmergency failed", typ) + logging.debug("%s to firEmergency failed", typ, exc_info=True) + # Without connection, plugin couldn't work + return else: logging.warning("Invalid Typ: %s", typ) finally: logging.debug("close firEmergency-Connection") - firSocket.close() + try: + firSocket.close() + except: + pass except: - logging.exception("unknown error") \ No newline at end of file + logging.error("unknown error") + logging.debug("unknown error", exc_info=True) \ No newline at end of file diff --git a/plugins/httpRequest/httpRequest.py b/plugins/httpRequest/httpRequest.py index da48450..7c5c761 100644 --- a/plugins/httpRequest/httpRequest.py +++ b/plugins/httpRequest/httpRequest.py @@ -36,9 +36,6 @@ def run(typ,freq,data): @requires: httpRequest-Configuration has to be set in the config.ini @return: nothing - @exception: Exception if ConfigParser failed - @exception: Exception if http Request failed - @exception: Exception if http Response failed """ try: # @@ -49,7 +46,10 @@ def run(typ,freq,data): for key,val in globals.config.items("httpRequest"): logging.debug(" - %s = %s", key, val) except: - logging.exception("cannot read config file") + logging.error("cannot read config file") + logging.debug("cannot read config file", exc_info=True) + # Without config, plugin couldn't work + return try: # @@ -79,7 +79,10 @@ def run(typ,freq,data): httprequest.request("GET", url[5]) #send URL Querry per GET except: - logging.exception("cannot send HTTP request") + logging.error("cannot send HTTP request") + logging.debug("cannot send HTTP request", exc_info=True) + return + else: try: # @@ -91,11 +94,17 @@ def run(typ,freq,data): else: logging.warning("HTTP response: %s - %s" , str(httpresponse.status), str(httpresponse.reason)) except: #otherwise - logging.exception("cannot get HTTP response") + logging.error("cannot get HTTP response") + logging.debug("cannot get HTTP response", exc_info=True) + return finally: logging.debug("close HTTP-Connection") - httprequest.close() + try: + httprequest.close() + except: + pass except: - logging.exception("unknown error") \ No newline at end of file + logging.error("unknown error") + logging.debug("unknown error", exc_info=True) \ No newline at end of file diff --git a/plugins/jsonSocket/jsonSocket.py b/plugins/jsonSocket/jsonSocket.py index 69766a0..010dad6 100644 --- a/plugins/jsonSocket/jsonSocket.py +++ b/plugins/jsonSocket/jsonSocket.py @@ -69,6 +69,8 @@ def run(typ,freq,data): except: logging.error("cannot initialize %s-socket", globals.config.get("jsonSocket", "protocol")) logging.debug("cannot initialize %s-socket", globals.config.get("jsonSocket", "protocol"), exc_info=True) + # Without connection, plugin couldn't work + return else: # toDo is equals for all types, so only check if typ is supported @@ -83,13 +85,17 @@ def run(typ,freq,data): except: logging.error("%s to %s failed", typ, globals.config.get("jsonSocket", "protocol")) logging.debug("%s to %s failed", typ, globals.config.get("jsonSocket", "protocol"), exc_info=True) + return else: logging.warning("Invalid Typ: %s", typ) finally: logging.debug("close %s-Connection", globals.config.get("jsonSocket", "protocol")) - sock.close() + try: + sock.close() + except: + pass except: # something very mysterious